RedisWorks, the Pythonic Redis Client.

illustrations illustrations illustrations illustrations illustrations illustrations

RedisWorks, the Pythonic Redis Client.

Published on Aug 10, 2016 by Sep Dehpour

Table Of Contents

As a person who loves dynamic typing, I have always been frustrated with Py-Redis. It is a great library, but it is too low level. It maps the actual Redis commands directly.

I always wanted to just say the.path = "something" and have it magically saved into Redis. Then somewhere else say print(the.path) and get the value from Redis. As simple as that! And here it is: RedisWorks

Install 

pip install redisworks

Note that RedisWorks needs Redis server 2.4+.

Setup 

let’s say if you want all the keys in Redis to start with the word root. Then you:

root = Root()  # connects to Redis on local host by default

Or if you want to be more specific:

root = Root(host='localhost', port=6379, db=0)

Saving to Redis 

Saving to Redis is as simple as assigning objects to attributes of root or attributes of attributes of root (you can go as deep as you want.) Make sure you are not using any Python’s reserved words in the key’s name.

Example:

>>> from redisworks import Root
>>> import datetime
>>> root = Root()
>>> root.my.list = [1, 3, 4]
>>> root.my.other.list = [1, [2, 2]]
>>> 
>>> some_date = datetime.datetime(2016, 8, 22, 10, 3, 19)
>>> root.time = some_date
>>> 
>>> root.the.mapping.example = {1:1, "a": {"b": 10}}

Redis works will automatically convert your object to the proper Redis type and immediately write it to Redis as soon as you assign an element!

The respective keys for the above items will be just like what you type: root.my.list, root.time, root.the.mapping.example:

If you use redis-cli, you will notice that the data is saved in the proper Redis data type:

127.0.0.1:6379> scan 0
1) "0"
2) 1) "root.the.mapping.example"
   2) "root.time"
   3) "root.my.list"
127.0.0.1:6379> type root.the.mapping.example
hash
127.0.0.1:6379> type root.time
string
127.0.0.1:6379> type root.my.list
list

Reading from Redis 

Reading the data is as simple as if it was just saved in Python memory!

Redis works returns Lazy queries just like how Django returns lazy queries. In fact the lazy objects code is borrowed from Django!

If you ran the example from Saving to Redis, run a flush root.flush() to empty Redisworks Cache. This is so it goes and gets the objects from Redis instead of reading its own current copy of data:

>>> from redisworks import Root
>>> import datetime
>>> root = Root()
>>> thetime = root.time
>>> thelist = root.my.list
>>> mydict = root.the.mapping.example
>>> mydict  # is not evalurated yet!
<Lazy object: root.the.mapping.example>
>>> print(mydict)
{1:1, "a": {"b": 10}}  # Now all the 3 objects are read from Redis!
>>> mydict
{1:1, "a": {"b": 10}}
>>> root.my.list
[1, 3, 4]
>>> root.my.other.list
[1, [2, 2]]
>>> root.time
2016-08-22 10:03:19

Changing root key name 

Every key name by default starts with the word root. If you want to use another name, simple subclass Root:

>>> from redisworks import Root
>>> post=Post()
>>> post.item1 = "something"  # saves to Redis
...
>>> print(post.item1)  # loads from Redis
something

Numbers as attribute names 

Let’s say you want root.1 as a key name. Python does not allow attribute names start with numbers.

All you need to do is start the number with the character i so Redisworks takes care of it for you:

>>> root.i1 = 10
>>> print(root.i1)
10

The actual key in Redis will be root.1

Use it, let me know how it goes! 

I’m pretty excited about this library. I learned a lot while making it and I will be speaking about it in the SoCal Python meetup on Aug 30 2016. Check it out if you are around!

Sep

See Also

Amazon S3 made simple for Python apps.

From time to time I need to do some simple file/folder transfer/query from a Python app to a S3 bucket. There is a great Python library called Boto that offers a comprehensive low level interface to AWS. I consider it too low level for simple operations on S3. On the other hand there is a fantastic command line library called s3cmd written in Python but it is designed to be used from command line and not from Python apps. Every other Python library I found to be dealing with S3 buckets, seemed unmaintained.

Read More
Diff It To Digg It

Diff It To Digg It

Anybody who has used git diff will know that your life is not the same once you start diffing. When you get to the habit, there is no going back. Now let’s look at diff for structured data!

Read More