diff options
author | Andy McCurdy <andy@andymccurdy.com> | 2010-03-16 10:29:41 -0700 |
---|---|---|
committer | Andy McCurdy <andy@andymccurdy.com> | 2010-03-16 10:29:41 -0700 |
commit | dbaef302fb95da2f4d30e5449891fc72eb0942d7 (patch) | |
tree | e47831d03ed734a9919eee486344b62397d73d76 | |
parent | d240e77d58c2cf686bef925cab93b5970129aeb7 (diff) | |
download | redis-py-dbaef302fb95da2f4d30e5449891fc72eb0942d7.tar.gz |
HKEYS HVALS and HGETALL implemented
-rw-r--r-- | redis/client.py | 16 | ||||
-rw-r--r-- | tests/server_commands.py | 45 |
2 files changed, 61 insertions, 0 deletions
diff --git a/redis/client.py b/redis/client.py index c5b3d52..006459d 100644 --- a/redis/client.py +++ b/redis/client.py @@ -153,6 +153,10 @@ def parse_info(response): info[key] = get_value(value) return info +def pairs_to_dict(response): + "Create a dict given a list of key/value pairs" + return dict(zip(response[::2], map(float, response[1::2]))) + def zset_score_pairs(response, **options): """ If ``withscores`` is specified in the options, return the response as @@ -202,6 +206,7 @@ class Redis(threading.local): string_keys_to_dict('ZRANGE ZRANGEBYSCORE ZREVRANGE', zset_score_pairs), { 'BGSAVE': lambda r: r == 'Background saving started', + 'HGETALL': lambda r: r and pairs_to_dict(r) or None, 'INFO': parse_info, 'LASTSAVE': timestamp_to_datetime, 'PING': lambda r: r == 'PONG', @@ -920,6 +925,14 @@ class Redis(threading.local): "Return the value of ``key`` within the hash ``name``" return self.format_bulk('HGET', name, key) + def hgetall(self, name): + "Return a Python dict of the hash's name/value pairs" + return self.format_inline('HGETALL', name) + + def hkeys(self, name): + "Return the list of keys within hash ``name``" + return self.format_inline('HKEYS', name) + def hset(self, name, key, value): """ Set ``key`` to ``value`` within hash ``name`` @@ -927,6 +940,9 @@ class Redis(threading.local): """ return self.format_multi_bulk('HSET', name, key, value) + def hvals(self, name): + "Return the list of values within hash ``name``" + return self.format_inline('HVALS', name) class Pipeline(Redis): """ diff --git a/tests/server_commands.py b/tests/server_commands.py index c957381..53f6ecf 100644 --- a/tests/server_commands.py +++ b/tests/server_commands.py @@ -728,6 +728,51 @@ class ServerCommandsTestCase(unittest.TestCase): self.assert_(self.client.hdel('a', 'a2')) self.assertEquals(self.client.hget('a', 'a2'), None) + def test_hgetall(self): + # key is not a hash + self.client['a'] = 'a' + self.assertRaises(redis.ResponseError, self.client.hgetall, 'a') + del self.client['a'] + # no key + self.assertEquals(self.client.hgetall('a'), None) + # real logic + h = {'a1': 1, 'a2': 2, 'a3': 3} + self.make_hash('a', h) + remote_hash = self.client.hgetall('a') + self.assertEquals(h, remote_hash) + + + def test_hkeys(self): + # key is not a hash + self.client['a'] = 'a' + self.assertRaises(redis.ResponseError, self.client.hkeys, 'a') + del self.client['a'] + # no key + self.assertEquals(self.client.hkeys('a'), None) + # real logic + h = {'a1': '1', 'a2': '2', 'a3': '3'} + self.make_hash('a', h) + keys = h.keys() + keys.sort() + remote_keys = self.client.hkeys('a') + remote_keys.sort() + self.assertEquals(keys, remote_keys) + + def test_hvals(self): + # key is not a hash + self.client['a'] = 'a' + self.assertRaises(redis.ResponseError, self.client.hvals, 'a') + del self.client['a'] + # no key + self.assertEquals(self.client.hvals('a'), None) + # real logic + h = {'a1': '1', 'a2': '2', 'a3': '3'} + self.make_hash('a', h) + vals = h.values() + vals.sort() + remote_vals = self.client.hvals('a') + remote_vals.sort() + self.assertEquals(vals, remote_vals) # SORT def test_sort_bad_key(self): |