Skip to content

Commit d7678e0

Browse files
committed
allow hmget to accept *args or a list of keys
1 parent 43f5e96 commit d7678e0

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

redis/client.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
def list_or_args(keys, args):
1616
# returns a single list combining keys and args
1717
try:
18-
i = iter(keys)
18+
iter(keys)
1919
# a string can be iterated, but indicates
2020
# keys wasn't passed as a list
2121
if isinstance(keys, basestring):
@@ -457,8 +457,8 @@ def mget(self, keys, *args):
457457
"""
458458
Returns a list of values ordered identically to ``keys``
459459
"""
460-
keys = list_or_args(keys, args)
461-
return self.execute_command('MGET', *keys)
460+
args = list_or_args(keys, args)
461+
return self.execute_command('MGET', *args)
462462

463463
def mset(self, mapping):
464464
"Sets each key in the ``mapping`` dict to its corresponding value"
@@ -777,29 +777,29 @@ def scard(self, name):
777777

778778
def sdiff(self, keys, *args):
779779
"Return the difference of sets specified by ``keys``"
780-
keys = list_or_args(keys, args)
781-
return self.execute_command('SDIFF', *keys)
780+
args = list_or_args(keys, args)
781+
return self.execute_command('SDIFF', *args)
782782

783783
def sdiffstore(self, dest, keys, *args):
784784
"""
785785
Store the difference of sets specified by ``keys`` into a new
786786
set named ``dest``. Returns the number of keys in the new set.
787787
"""
788-
keys = list_or_args(keys, args)
789-
return self.execute_command('SDIFFSTORE', dest, *keys)
788+
args = list_or_args(keys, args)
789+
return self.execute_command('SDIFFSTORE', dest, *args)
790790

791791
def sinter(self, keys, *args):
792792
"Return the intersection of sets specified by ``keys``"
793-
keys = list_or_args(keys, args)
794-
return self.execute_command('SINTER', *keys)
793+
args = list_or_args(keys, args)
794+
return self.execute_command('SINTER', *args)
795795

796796
def sinterstore(self, dest, keys, *args):
797797
"""
798798
Store the intersection of sets specified by ``keys`` into a new
799799
set named ``dest``. Returns the number of keys in the new set.
800800
"""
801-
keys = list_or_args(keys, args)
802-
return self.execute_command('SINTERSTORE', dest, *keys)
801+
args = list_or_args(keys, args)
802+
return self.execute_command('SINTERSTORE', dest, *args)
803803

804804
def sismember(self, name, value):
805805
"Return a boolean indicating if ``value`` is a member of set ``name``"
@@ -827,16 +827,16 @@ def srem(self, name, *values):
827827

828828
def sunion(self, keys, *args):
829829
"Return the union of sets specifiued by ``keys``"
830-
keys = list_or_args(keys, args)
831-
return self.execute_command('SUNION', *keys)
830+
args = list_or_args(keys, args)
831+
return self.execute_command('SUNION', *args)
832832

833833
def sunionstore(self, dest, keys, *args):
834834
"""
835835
Store the union of sets specified by ``keys`` into a new
836836
set named ``dest``. Returns the number of keys in the new set.
837837
"""
838-
keys = list_or_args(keys, args)
839-
return self.execute_command('SUNIONSTORE', dest, *keys)
838+
args = list_or_args(keys, args)
839+
return self.execute_command('SUNIONSTORE', dest, *args)
840840

841841

842842
#### SORTED SET COMMANDS ####
@@ -1089,9 +1089,10 @@ def hmset(self, name, mapping):
10891089
items.extend(pair)
10901090
return self.execute_command('HMSET', name, *items)
10911091

1092-
def hmget(self, name, keys):
1092+
def hmget(self, name, keys, *args):
10931093
"Returns a list of values ordered identically to ``keys``"
1094-
return self.execute_command('HMGET', name, *keys)
1094+
args = list_or_args(keys, args)
1095+
return self.execute_command('HMGET', name, *args)
10951096

10961097
def hvals(self, name):
10971098
"Return the list of values within hash ``name``"

tests/server_commands.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,8 @@ def test_hmget(self):
10031003
self.assert_(self.client.hmset('foo', d))
10041004
self.assertEqual(self.client.hmget('foo', ['a', 'b', 'c']), ['1', '2', '3'])
10051005
self.assertEqual(self.client.hmget('foo', ['a', 'c']), ['1', '3'])
1006+
# using *args type args
1007+
self.assertEquals(self.client.hmget('foo', 'a', 'c'), ['1', '3'])
10061008

10071009
def test_hmget_empty(self):
10081010
self.assertEqual(self.client.hmget('foo', ['a', 'b']), [None, None])

0 commit comments

Comments
 (0)