Permalink
Browse files

keys() values() and items() with some documentation

  • Loading branch information...
1 parent fe5009c commit 60776e1b986c4dfe9058bb43fb3a5e2bde614c59 @atl atl committed Sep 14, 2010
Showing with 40 additions and 4 deletions.
  1. +18 −1 docs/proxy.rst
  2. +22 −3 redish/proxy.py
View
@@ -224,7 +224,21 @@ database, allowing you to treat the keyspace as a dict::
>>> names[1]
u'Bob'
-These features can be combined::
+Not only can you get ``keys`` that match a (glob-style) pattern, as in
+``redis.keys``(), but you can also get ``values`` and ``items``. When
+fed a keyspace label as an argument, the formatstring is converted to
+a glob-style pattern. When used with keyspaced proxies, no argument is
+needed, and the keyspace's formatstring is converted into a glob-style
+pattern. The following are thus equivalent::
+
+ >>> r.keys('person:*:name')
+ ['person:0001:name']
+ >>> r.keys('myspace')
+ ['person:0001:name']
+ >>> names.keys()
+ ['person:0001:name']
+
+All these features can be combined::
>>> ZZ = x.register_keyspace('friends', '%(type)s:%(id)04d:friends')
>>> friendstore = x.keyspace(ZZ)
@@ -238,6 +252,9 @@ These features can be combined::
<Set: ['1', '204']>
>>> x['pet:0204:friends'].intersection(friendstore[frank])
set(['1'])
+ >>> friendstore.items()
+ [('person:0203:friends', <Set: ['1', '204']>),
+ ('pet:0204:friends', <Set: ['1', '202']>)]
I have no idea at this point if these experimental features are
useful, but they are small, and seem to make sense to me. Feedback is
View
@@ -149,11 +149,20 @@ def __delitem__(self, k):
del self._empties[key]
self.delete(*keys)
- def multikey(self, pattern):
+ def values(self, pattern):
if pattern in self._keyspaces:
pattern = FORMAT_SPEC.sub('*', self._keyspaces[pattern])
- for p in self.keys(pattern):
- yield self[p]
+ return [self[p] for p in super(Proxy, self).keys(pattern)]
+
+ def keys(self, pattern):
+ if pattern in self._keyspaces:
+ pattern = FORMAT_SPEC.sub('*', self._keyspaces[pattern])
+ return super(Proxy, self).keys(pattern)
+
+ def items(self, pattern):
+ if pattern in self._keyspaces:
+ pattern = FORMAT_SPEC.sub('*', self._keyspaces[pattern])
+ return [(p, self[p]) for p in super(Proxy, self).keys(pattern)]
def register_keyspace(self, shortcut, formatstring):
"""
@@ -195,6 +204,7 @@ class KeyspacedProxy(Proxy):
def __init__(self, proxy, transform):
self.proxy = proxy
self.transform = transform
+ self.globbed = FORMAT_SPEC.sub('*', transform)
def __getitem__(self, key):
return self.proxy.__getitem__(self.transform % key)
@@ -208,3 +218,12 @@ def __contains__(self, key):
def __delitem__(self, key):
return self.proxy.__delitem__(self.transform % key)
+ def keys(self):
+ return self.proxy.keys(self.globbed)
+
+ def values(self):
+ return self.proxy.values(self.globbed)
+
+ def items(self):
+ return self.proxy.items(self.globbed)
+

0 comments on commit 60776e1

Please sign in to comment.