Meet the Dictator! Small and kind

    Prologue


    In my work, from time to time, I have a desire to change the behavior of this or that tool: to make working with it more familiar, the API more transparent, etc. This happened when I inherited a project where Redis was used as storage. Undoubtedly, Python has enough libraries for convenient work with Redis, however, remembering that this is a key-value storage, I couldn’t help but think of how wonderful it would be to work with it as a regular Python dictionary (dict) .



    Instead


    >>> redis_obj.exist(key)

    use


    >>> key in redis_obj

    Save objects with habitual assignment:


    >>> redis_obj['Planets'] = ['Mercury', 'Venus', 'Earth', 'Mars']

    And so was born Dictator. A dictator mimics the methods of the dict class by hiding the Redis interface from the user.


    Installation


    Installation is extremely simple, like many Python libraries, it is available in Pypi


    $ pip install dictator

    MIT licensed source code to be taken to storage on GitHub.


    Using


    Using Dictator is extremely simple, but initializing objects is still different from the dictionary (you need to know where to connect):


    >>> dc = Dictator(host='localhost', port=6379, db=0)

    Further, you can work with the created object in the usual manner:


    • .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 a value in case keynothing is found by the key


      >>> 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)


      >>> dc.delete('Comets')

      or


      >>> del dc['Comets']

    • .keys() and .values()


      >>> dc.keys()
      ['Planets', 'Stars']
      >>> dc.values()
      [['Mercury', 'Venus', 'Earth']]

    • .items()


      >>> dc.iterms()
      [('Planets', ['Mercury', 'Venus', 'Earth'])]

    • and of course iteration using the generator object:


      • .iterkeys()
      • .itervalues()
      • .iteritems()


    And that's not it :)


    Epilogue


    The project is in its infancy, carefully documented and decorated.
    Forks, pull requests and other issues are welcome!


    Github: dictator


    Thanks for attention!


    Also popular now: