Skip to content

Instantly share code, notes, and snippets.

@wolkym
Forked from vladignatyev/dict_to_redis.py
Created February 28, 2017 08:05
Show Gist options
  • Save wolkym/4f458c1f54d6b3dd2a3b2374da275346 to your computer and use it in GitHub Desktop.
Save wolkym/4f458c1f54d6b3dd2a3b2374da275346 to your computer and use it in GitHub Desktop.
Save Python dict to Redis hash
def dict_to_redis_hset(r, hkey, dict_to_store):
"""
Saves `dict_to_store` dict into Redis hash, where `hkey` is key of hash.
>>> import redis
>>> r = redis.StrictRedis(host='localhost')
>>> d = {'a':1, 'b':7, 'foo':'bar'}
>>> dict_to_redis_hset(r, 'test', d)
True
>>> r.hgetall('test')
{'a':1, 'b':7, 'foo':'bar'}
"""
return all([r.hset(hkey, k, v) for k, v in dict_to_store.items()])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment