Skip to content

dictatorlib/dictator

Repository files navigation

Build Status Python version Python version license Codacy Badge Lintly

Dictator

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.

Install

$ pip install dictator

Usage

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 normal str instead of bytes.

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 a dict-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 standard dict:

    >>> 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 of Dictator