Skip to content

Commit dbaef30

Browse files
committed
HKEYS HVALS and HGETALL implemented
1 parent d240e77 commit dbaef30

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

redis/client.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ def get_value(value):
153153
info[key] = get_value(value)
154154
return info
155155

156+
def pairs_to_dict(response):
157+
"Create a dict given a list of key/value pairs"
158+
return dict(zip(response[::2], map(float, response[1::2])))
159+
156160
def zset_score_pairs(response, **options):
157161
"""
158162
If ``withscores`` is specified in the options, return the response as
@@ -202,6 +206,7 @@ class Redis(threading.local):
202206
string_keys_to_dict('ZRANGE ZRANGEBYSCORE ZREVRANGE', zset_score_pairs),
203207
{
204208
'BGSAVE': lambda r: r == 'Background saving started',
209+
'HGETALL': lambda r: r and pairs_to_dict(r) or None,
205210
'INFO': parse_info,
206211
'LASTSAVE': timestamp_to_datetime,
207212
'PING': lambda r: r == 'PONG',
@@ -920,13 +925,24 @@ def hget(self, name, key):
920925
"Return the value of ``key`` within the hash ``name``"
921926
return self.format_bulk('HGET', name, key)
922927

928+
def hgetall(self, name):
929+
"Return a Python dict of the hash's name/value pairs"
930+
return self.format_inline('HGETALL', name)
931+
932+
def hkeys(self, name):
933+
"Return the list of keys within hash ``name``"
934+
return self.format_inline('HKEYS', name)
935+
923936
def hset(self, name, key, value):
924937
"""
925938
Set ``key`` to ``value`` within hash ``name``
926939
Returns 1 if HSET created a new field, otherwise 0
927940
"""
928941
return self.format_multi_bulk('HSET', name, key, value)
929942

943+
def hvals(self, name):
944+
"Return the list of values within hash ``name``"
945+
return self.format_inline('HVALS', name)
930946

931947
class Pipeline(Redis):
932948
"""

tests/server_commands.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,51 @@ def test_hdel(self):
728728
self.assert_(self.client.hdel('a', 'a2'))
729729
self.assertEquals(self.client.hget('a', 'a2'), None)
730730

731+
def test_hgetall(self):
732+
# key is not a hash
733+
self.client['a'] = 'a'
734+
self.assertRaises(redis.ResponseError, self.client.hgetall, 'a')
735+
del self.client['a']
736+
# no key
737+
self.assertEquals(self.client.hgetall('a'), None)
738+
# real logic
739+
h = {'a1': 1, 'a2': 2, 'a3': 3}
740+
self.make_hash('a', h)
741+
remote_hash = self.client.hgetall('a')
742+
self.assertEquals(h, remote_hash)
743+
744+
745+
def test_hkeys(self):
746+
# key is not a hash
747+
self.client['a'] = 'a'
748+
self.assertRaises(redis.ResponseError, self.client.hkeys, 'a')
749+
del self.client['a']
750+
# no key
751+
self.assertEquals(self.client.hkeys('a'), None)
752+
# real logic
753+
h = {'a1': '1', 'a2': '2', 'a3': '3'}
754+
self.make_hash('a', h)
755+
keys = h.keys()
756+
keys.sort()
757+
remote_keys = self.client.hkeys('a')
758+
remote_keys.sort()
759+
self.assertEquals(keys, remote_keys)
760+
761+
def test_hvals(self):
762+
# key is not a hash
763+
self.client['a'] = 'a'
764+
self.assertRaises(redis.ResponseError, self.client.hvals, 'a')
765+
del self.client['a']
766+
# no key
767+
self.assertEquals(self.client.hvals('a'), None)
768+
# real logic
769+
h = {'a1': '1', 'a2': '2', 'a3': '3'}
770+
self.make_hash('a', h)
771+
vals = h.values()
772+
vals.sort()
773+
remote_vals = self.client.hvals('a')
774+
remote_vals.sort()
775+
self.assertEquals(vals, remote_vals)
731776

732777
# SORT
733778
def test_sort_bad_key(self):

0 commit comments

Comments
 (0)