Dictator is a tiny library for Robots™ to work with Redis as a Dict.
It handles Redis API commands and represent itself as a dict-like object.
$ pip install dictator
It's easy to start by creating Dictator
object
>>> dc = Dictator(host='localhost', port=6379, db=0)
For Python 3 it's useful to add
decode_responses=True
to constructor to get normalstr
instead ofbytes
.
For the moment it handles not all features of Python Dict but basics:
-
.set(key, value)
>>> dc.set('Planets', ['Mercury', 'Venus', 'Earth']) >>> dc['Stars'] = ['Sun']
-
.get(key)
>>> dc['Stars'] ['Sun'] >>> dc.get('Planets') ['Mercury', 'Venus', 'Earth']
You can set default value for
get()
function just like for adict
-object:>>> dc.get('Comets', 'No data') 'No data'
-
.update(other=None, **kwargs)
>>> dc.update({'Stars': ['Sun', 'Vega']}) >>> dc.update(Stars=['Sun'])
-
.pop(key, default=None)
>>> dc.pop('Stars') ['Sun'] >>> dc.pop('Comets')
-
delete key from Dictator
>>> del dc['Comets']
-
.keys()
and.values()
>>> dc.keys() ['Planets', 'Stars'] >>> dc.values() [['Mercury', 'Venus', 'Earth']]
-
.items()
>>> dc.items() [('Planets', ['Mercury', 'Venus', 'Earth'])]
-
len()
>>> len(dc) 1
-
also it supports iteration via generator object:
.iterkeys()
.itervalues()
.iteritems()
-
a copy of a
Dictator
object will be Python's standarddict
:>>> from copy import copy, deepcopy >>> d = dc.copy() >>> d {'Planets': ['Mercury', 'Venus', 'Earth']} >>> type(d) dict >>> copy(dc) == deepcopy(dc) == dc.copy() True
-
plus all methods of redis-py
Redis
instance can be applied to an instance ofDictator