Skip to content

Instantly share code, notes, and snippets.

@rcalsaverini
Last active August 29, 2015 14:07
Show Gist options
  • Save rcalsaverini/875ab047377fcde18ae5 to your computer and use it in GitHub Desktop.
Save rcalsaverini/875ab047377fcde18ae5 to your computer and use it in GitHub Desktop.
Redis + Lua
import redis
import functools
from collections import namedtuple
# TODO:
# - better to implement lua methods as descriptors?
# - get lua code from *.lua files
class BaseEvaluator(object):
LuaFunction = namedtuple("LuaFunction", ["name", "code", "nkeys", "docs"])
def __init__(self, code, redis_client):
self.redis_client = redis_client
def __getitem__(self, method):
raise KeyError("Not Implemented")
def __contains__(self, method):
raise KeyError("Not Implemented")
def evaluate(self, lua_function, *args):
return self.redis_client.eval(lua_function.code, lua_function.nkeys, *args)
def __getattr__(self, method_name):
if method_name in self:
lua_function = self[name]
def function(*args):
return self.evaluate(lua_function)
function.__name__ = lua_function.name
function.__doc__ = lua_function.docs
return function
else:
msg = "{} has no method called {}".format(self.__class__.__name__, method_name)
raise AttributeError(msg)
class EvaluatorFromDir(Evaluator):
def __init__(self, code_dir, redis_client):
self.definitions =
@rcalsaverini
Copy link
Author

Starting to play with redis + lua.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment