Skip to content

Commit

Permalink
renamed zinter/zunion to zinterstore/zunionstore to stay consistent w…
Browse files Browse the repository at this point in the history
…ith Redis.
  • Loading branch information
andymccurdy committed May 23, 2010
1 parent 2b07bd7 commit dd84212
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
16 changes: 14 additions & 2 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,12 +925,18 @@ def zincrby(self, name, value, amount=1):
return self.execute_command('ZINCRBY', name, amount, value)

def zinter(self, dest, keys, aggregate=None):
warnings.warn(DeprecationWarning(
"Redis.zinter has been deprecated, use Redis.zinterstore instead"
))
return self.zinterstore(dest, keys, aggregate)

def zinterstore(self, dest, keys, aggregate=None):
"""
Intersect multiple sorted sets specified by ``keys`` into
a new sorted set, ``dest``. Scores in the destination will be
aggregated based on the ``aggregate``, or SUM if none is provided.
"""
return self._zaggregate('ZINTER', dest, keys, aggregate)
return self._zaggregate('ZINTERSTORE', dest, keys, aggregate)

def zrange(self, name, start, end, desc=False, withscores=False):
"""
Expand Down Expand Up @@ -1017,12 +1023,18 @@ def zscore(self, name, value):
return self.execute_command('ZSCORE', name, value)

def zunion(self, dest, keys, aggregate=None):
warnings.warn(DeprecationWarning(
"Redis.zunion has been deprecated, use Redis.zunionstore instead"
))
return self.zunionstore(dest, keys, aggregate)

def zunionstore(self, dest, keys, aggregate=None):
"""
Union multiple sorted sets specified by ``keys`` into
a new sorted set, ``dest``. Scores in the destination will be
aggregated based on the ``aggregate``, or SUM if none is provided.
"""
return self._zaggregate('ZUNION', dest, keys, aggregate)
return self._zaggregate('ZUNIONSTORE', dest, keys, aggregate)

def _zaggregate(self, command, dest, keys, aggregate=None):
pieces = [command, dest, len(keys)]
Expand Down
20 changes: 12 additions & 8 deletions tests/server_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,27 +621,29 @@ def test_zincrby(self):
self.assertEquals(self.client.zscore('a', 'a2'), 3.0)
self.assertEquals(self.client.zscore('a', 'a3'), 8.0)

def test_zinter(self):
def test_zinterstore(self):
self.make_zset('a', {'a1': 1, 'a2': 1, 'a3': 1})
self.make_zset('b', {'a1': 2, 'a3': 2, 'a4': 2})
self.make_zset('c', {'a1': 6, 'a3': 5, 'a4': 4})

# sum, no weight
self.assert_(self.client.zinter('z', ['a', 'b', 'c']))
self.assert_(self.client.zinterstore('z', ['a', 'b', 'c']))
self.assertEquals(
self.client.zrange('z', 0, -1, withscores=True),
[('a3', 8), ('a1', 9)]
)

# max, no weight
self.assert_(self.client.zinter('z', ['a', 'b', 'c'], aggregate='MAX'))
self.assert_(
self.client.zinterstore('z', ['a', 'b', 'c'], aggregate='MAX')
)
self.assertEquals(
self.client.zrange('z', 0, -1, withscores=True),
[('a3', 5), ('a1', 6)]
)

# with weight
self.assert_(self.client.zinter('z', {'a': 1, 'b': 2, 'c': 3}))
self.assert_(self.client.zinterstore('z', {'a': 1, 'b': 2, 'c': 3}))
self.assertEquals(
self.client.zrange('z', 0, -1, withscores=True),
[('a3', 20), ('a1', 23)]
Expand Down Expand Up @@ -764,27 +766,29 @@ def test_zscore(self):
# test a non-existant member
self.assertEquals(self.client.zscore('a', 'a4'), None)

def test_zunion(self):
def test_zunionstore(self):
self.make_zset('a', {'a1': 1, 'a2': 1, 'a3': 1})
self.make_zset('b', {'a1': 2, 'a3': 2, 'a4': 2})
self.make_zset('c', {'a1': 6, 'a4': 5, 'a5': 4})

# sum, no weight
self.assert_(self.client.zunion('z', ['a', 'b', 'c']))
self.assert_(self.client.zunionstore('z', ['a', 'b', 'c']))
self.assertEquals(
self.client.zrange('z', 0, -1, withscores=True),
[('a2', 1), ('a3', 3), ('a5', 4), ('a4', 7), ('a1', 9)]
)

# max, no weight
self.assert_(self.client.zunion('z', ['a', 'b', 'c'], aggregate='MAX'))
self.assert_(
self.client.zunionstore('z', ['a', 'b', 'c'], aggregate='MAX')
)
self.assertEquals(
self.client.zrange('z', 0, -1, withscores=True),
[('a2', 1), ('a3', 2), ('a5', 4), ('a4', 5), ('a1', 6)]
)

# with weight
self.assert_(self.client.zunion('z', {'a': 1, 'b': 2, 'c': 3}))
self.assert_(self.client.zunionstore('z', {'a': 1, 'b': 2, 'c': 3}))
self.assertEquals(
self.client.zrange('z', 0, -1, withscores=True),
[('a2', 1), ('a3', 5), ('a5', 12), ('a4', 19), ('a1', 23)]
Expand Down

0 comments on commit dd84212

Please sign in to comment.