Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First pass on proxy namespaces

  • Loading branch information...
commit de6abab1e9a2c1481258bd2e4377c7d0a675256b 1 parent 04b22ef
@atl atl authored
Showing with 26 additions and 0 deletions.
  1. +26 −0 redish/proxy.py
View
26 redish/proxy.py
@@ -53,8 +53,19 @@ def __init__(self, *args, **kwargs):
subsequent accesses keep the right type without throwing KeyErrors.
"""
self.empties = {}
+ self.keyspaces = {}
super(Proxy, self).__init__(*args, **kwargs)
+ def keyspaced(f):
+ def preprocessed(self, key, *argv):
+ if isinstance(key, tuple):
+ keyspace = key[0]
+ keyargs = tuple(key[1:])
+ key = self.keyspaces[keyspace] % keyargs
+ return f(self, key, *argv)
+ return preprocessed
+
+ @keyspaced
def __getitem__(self, key):
"""Return a proxy type according to the native redis type
associated with the key."""
@@ -74,8 +85,13 @@ def __getitem__(self, key):
else:
return TYPE_MAP[typ](key, self)
+ @keyspaced
def __setitem__(self, key, value):
"""Copy the contents of the value into the redis store."""
+ if isinstance(key, tuple):
+ keyspace = key[0]
+ keyargs = tuple(key[1:])
+ key = self.keyspaces[keyspace] % keyargs
if key in self.empties:
del self.empties[key]
if isinstance(value, (int, types.Int)):
@@ -106,6 +122,7 @@ def __setitem__(self, key, value):
pline = pline.zadd(key, k, v)
pline.execute()
+ @keyspaced
def __contains__(self, key):
"""
We check for existence within the *proxy object*, and so we
@@ -113,6 +130,7 @@ def __contains__(self, key):
"""
return self.exists(key) or key in self.empties
+ @keyspaced
def __delitem__(self, k):
if isinstance(k, Glob):
keys = self.keys(k)
@@ -127,3 +145,11 @@ def multikey(self, pattern):
for p in self.keys(pattern):
yield self[p]
+ def register_keyspace(self, shortcut, formatstring):
+ self.keyspaces[shortcut] = formatstring
+ return shortcut
+
+ @keyspaced
+ def echo_key(self, key):
+ print key
+
Please sign in to comment.
Something went wrong with that request. Please try again.