Permalink
Browse files

Added SortedSet.keysview, .itemsview

Also items now supports the desc and withscores arguments
  • Loading branch information...
1 parent 08d6b3a commit 0d30a856c70da9647ce321a9a8469ab9265a1fc8 Ask Solem committed Jul 5, 2010
Showing with 76 additions and 4 deletions.
  1. +28 −0 redish/tests/test_types.py
  2. +48 −4 redish/types.py
View
@@ -310,6 +310,34 @@ def test_range_by_score(self):
self.assertListEqual(z.range_by_score(0.3, 1.0), [
"baz", "zaz", "foo"])
+ def test_itemsview(self):
+ data = (("foo", 0.9), ("bar", 0.1), ("baz", 0.3),
+ ("bam", 1.2), ("xuzzy", 0.2), ("zaz", 0.4))
+ z = self.client.SortedSet("test:SortedSet:itemsview", data)
+ view = z.itemsview()
+ self.assertTupleEqual(tuple(view), (
+ ("bar", 0.1),
+ ("xuzzy", 0.2),
+ ("baz", 0.3),
+ ("zaz", 0.4),
+ ("foo", 0.9),
+ ("bam", 1.2)))
+
+ def test_keysview(self):
+ data = (("foo", 0.9), ("bar", 0.1), ("baz", 0.3),
+ ("bam", 1.2), ("xuzzy", 0.2), ("zaz", 0.4))
+ z = self.client.SortedSet("test:SortedSet:keysview", data)
+ self.assertListEqual(list(z.keysview()),
+ ["bar", "xuzzy", "baz", "zaz", "foo", "bam"])
+ self.assertListEqual(list(reversed(z.keysview())),
+ ["bam", "foo", "zaz", "baz", "xuzzy", "bar"])
+ self.assertListEqual(z.keysview()[0:3],
+ ["bar", "xuzzy", "baz"])
+
+ self.assertEqual(z.keysview()[3], "zaz")
+
+ self.assertRaises(IndexError, z.keysview().__getitem__, 100)
+
class test_Dict(ClientTestCase):
View
@@ -207,6 +207,36 @@ class SortedSet(Type):
"""
+ class _itemsview(object):
+
+ def __init__(self, zset, start=0, end=-1, desc=False,
+ withscores=False):
+ self.zset = zset
+ self.start = start
+ self.end = end
+ self.desc = desc
+ self.withscores = withscores
+
+ def _items(self, start, end, desc, withscores):
+ return self.zset.items(start, end, desc=desc,
+ withscores=withscores)
+
+ def __iter__(self):
+ return iter(self._items(self.start, self.end, self.desc,
+ self.withscores))
+
+ def __reversed__(self):
+ return self._items(self.start, self.end, True, self.withscores)
+
+ def __getitem__(self, s):
+ if isinstance(s, slice):
+ i = s.start or 0
+ j = s.stop or -1
+ j = j - 1
+ return self._items(i, j, False, self.withscores)
+ else:
+ return self._items(s, s, False, self.withscores)[0]
+
def __init__(self, name, client, initial=None):
super(SortedSet, self).__init__(name, client)
if initial:
@@ -267,10 +297,11 @@ def score(self, member):
"""Return the score associated with the specified member."""
return self.client.zscore(self.name, member)
- def range_by_score(self, min, max):
+ def range_by_score(self, min, max, num=None, withscores=False):
"""Return all the elements with score >= min and score <= max
(a range query) from the sorted set."""
- return self.client.zrangebyscore(self.name, min, max)
+ return self.client.zrangebyscore(self.name, min, max, num=num,
+ withscores=withscores)
def update(self, iterable):
for member, score in iterable:
@@ -279,8 +310,21 @@ def update(self, iterable):
def _as_set(self):
return self.client.zrange(self.name, 0, -1)
- def items(self):
- return self.client.zrange(self.name, 0, -1, withscores=True)
+ def items(self, start=0, end=-1, desc=False, withscores=False):
+ return self.client.zrange(self.name, start, end,
+ desc=desc, withscores=withscores)
+
+ def itemsview(self, start=0, end=-1, desc=False):
+ return self._itemsview(self, start, end, desc, withscores=True)
+
+ def keysview(self, start=0, end=-1, desc=False):
+ return self._itemsview(self, start, end, desc, withscores=False)
+
+
+
+
+
+
class Dict(Type):

0 comments on commit 0d30a85

Please sign in to comment.