Last active
August 29, 2015 14:07
-
-
Save rcalsaverini/875ab047377fcde18ae5 to your computer and use it in GitHub Desktop.
Redis + Lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Starting to play with redis + lua.