Skip to content

Commit

Permalink
HMSET support
Browse files Browse the repository at this point in the history
  • Loading branch information
kmerenkov authored and andymccurdy committed Apr 12, 2010
1 parent 4dec96f commit edcb36a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class Redis(threading.local):
"""
RESPONSE_CALLBACKS = dict_merge(
string_keys_to_dict(
'AUTH DEL EXISTS EXPIRE HDEL HEXISTS MOVE MSETNX RENAMENX '
'AUTH DEL EXISTS EXPIRE HDEL HEXISTS HMSET MOVE MSETNX RENAMENX '
'SADD SISMEMBER SMOVE SETNX SREM ZADD ZREM',
bool
),
Expand Down Expand Up @@ -1001,6 +1001,12 @@ def hset(self, name, key, value):
"""
return self.execute_command('HSET', name, key, value)

def hmset(self, key, mapping):
"Sets each key in the ``mapping`` dict to its corresponding value"
items = []
[items.extend(pair) for pair in mapping.iteritems()]
return self.execute_command('HMSET', key, *items)

def hvals(self, name):
"Return the list of values within hash ``name``"
return self.execute_command('HVALS', name)
Expand Down
7 changes: 7 additions & 0 deletions tests/server_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,13 @@ def test_hget_and_hset(self):
# key inside of hash that doesn't exist returns null value
self.assertEquals(self.client.hget('a', 'b'), None)

def test_hmset(self):
d = {'a': '1', 'b': '2', 'c': '3'}
self.assert_(self.client.hmset('foo', d))
self.assertEqual(self.client.hgetall('foo'), d)
self.assertRaises(redis.ResponseError, self.client.hmset, 'foo', {})


def test_hdel(self):
# key is not a hash
self.client['a'] = 'a'
Expand Down

0 comments on commit edcb36a

Please sign in to comment.