Redisworks 0.4.0
The Pythonic Redis Client
Why Redisworks?
Lazy Redis Queries
Dynamic Typing
Ease of use
Have you ever used PyRedis and wondered why you have to think about types all the time? That you have to constantly convert objects to strings and back and forth since Redis keeps most things as strings?
Redis works provides a Pythonic interface to Redis. Let Redisworks take care of type conversions for you.
Behind the scene, Redisworks uses DotObject to provide beautiful dot notation objects and lazy Redis queries.
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)
password
Any other parameter that you pass to Root will be passed down to PyRedis. For example:
root = Root(host='localhost', port=6379, db=0, password='mypass')
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
>>> 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, you have two options:
Option 1, pass a namespace:
>>> mynamespace = Root(conn=redis_conn, namespace='mynamespace')
>>> mynamespace.foo = 'bar'
Option 2, simply subclass Root:
>>> from redisworks import Root
>>> class Post(Root):
... pass
>>> 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
Dynamic key names
>>> path1 = 'blah'
>>> path2 = 'blah.here`'
>>> root[path1] = 'foo'
>>> root[path2] = 'foo bar'
>>> root.blah
foo
>>> root.blah.here
foo bar
Passing TTL to the keys
You can use the with_ttl helper.
>>> from redisworks import Root, with_ttl
>>> self.root.myset = with_ttl([1, 2, 3], ttl=1)
>>> self.root.flush()
>>> self.root.myset
[1, 2, 3]
>>> time.sleep(1.2)
>>> self.root.flush()
>>> self.root.myset
Other examples
Take a look at example.py
Metadata
Release files for redisworks 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| redisworks-0.4.0.tar.gz | 8.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| redisworks-0.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.2 kB
Release files / redisworks-0.4.0.tar.gz
| Download URL | redisworks-0.4.0.tar.gz |
|---|---|
| Size | 8.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f3f61ae47f91a194192ba815cd932ebc7c07129a386ca0b29081f6362e2cd088
|
|
BLAKE2b-256 checksum How to use checksums |
39f44a2015c56135422cd4a82e1b766791e29113b96564089616e0ad3d5ba8ee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.11.0
|
Release files / redisworks-0.4.0-py3-none-any.whl
| Download URL | redisworks-0.4.0-py3-none-any.whl |
|---|---|
| Size | 8.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
43a54fce68c211d55c445cbc99805905256a34fb797ef444ac8876ef7960a772
|
|
BLAKE2b-256 checksum How to use checksums |
1ccb8c148e0ad87f54cdf3fc89e0e842fad8c47d3e438f6cdf2368b6acd10e61
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.11.0
|