Skip to content

Instantly share code, notes, and snippets.

@appcove
Created March 4, 2013 07:50
Show Gist options
  • Save appcove/5080737 to your computer and use it in GitHub Desktop.
Save appcove/5080737 to your computer and use it in GitHub Desktop.
Illustrating the power of python.
###############################################################################
class SessionDictProxy():
'''
Emulates an aadict() object, but all has/get/set/del operations convert to/from
json objects stored in Redis.
'''
def __init__(self, Token, RedisKey):
object.__setattr__(self, '_Token', Token)
object.__setattr__(self, '_RedisKey', RedisKey)
def __contains__(self, key):
return App.Redis.hexists(self._RedisKey, key)
def __getitem__(self, key):
try:
return JD(App.Redis.hget_str(self._RedisKey, key))
except (TypeError, ValueError) as e:
raise KeyError("Key '{0}' could not be found or decoded from Redis: {1}".format(key, self._RedisKey))
def __setitem__(self, key, value):
App.Redis.hset_str(self._RedisKey, key, JE(value))
def __delitem__(self, key):
App.Redis.hdel(self._RedisKey, key)
__hasattr__ = __contains__
__getattr__ = __getitem__
__setattr__ = __setitem__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment