A convenience wrapper for the official Python redis package
Project description
Py-Redis
A convenience wrapper for the official Python Redis package. Supports the Python context manager protocol and takes care of (de-)serializing data to JSON, as well as helper methods to work on multiple keys.
Object Creation
from pyredis import RedisConnection
# pass everything you would pass to redis.Redis()
redis_args = {
'host': 'localhost',
# 'password': 'my$ecureRed1s',
# 'port': 1234,
}
with RedisConnection(**redis_args) as my_redis:
my_redis.set('key', 'value')
Redis Get and Set
# redis set
with RedisConnection(**redis_args) as my_redis:
my_redis.set('a_sting', 'my_sting value')
my_redis.set('a_list', [1, 4, 3, 2])
my_redis.set('a_dict', {'key_1': 'val_1', 'key_2': 'val_2'})
# redis get
with RedisConnection(**redis_args) as my_redis:
data = my_redis.get('a_dict')
# data is already converted to a dict
print(type(data))
Handle Lists and Dictionaries
from pprint import pprint
# get multiple keys / data
with RedisConnection(**redis_args) as my_redis:
# get all keys that start with a_
pattern = 'a_'
keys = my_redis.get_key_pattern(pattern)
print(f"list of all keys that start with {pattern}: {keys}")
data = my_redis.get_data_for_keys(keys)
print(f"data of all keys that start with {pattern}: {data}")
# or retrieve the data as a key: data dictionary for a specific pattern
print('data as key: data dictionary for a pattern:')
data = my_redis.get_keys('a_')
pprint(data)
# set all entries of a dictionary to redis
data = {'a': 12, 'b': 'myvalue'}
with RedisConnection(**redis_args) as my_redis:
# yo can continue working with the keys
keys = my_redis.set_dict(data)
print(my_redis.get('a'))
print(my_redis.get(keys[1]))
Fallback
# you can always work directly on the redis.Redis() object,
# as you would with the official package,
# by using the RedisConnection.R attribute
with RedisConnection(**redis_args) as my_redis:
print('access redis client through object...')
print(my_redis.R.get('a_dict'))
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
py-redis-1.0.4.tar.gz
(3.4 kB
view details)
Built Distribution
File details
Details for the file py-redis-1.0.4.tar.gz
.
File metadata
- Download URL: py-redis-1.0.4.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6a954dedbfc1718cc6fe58fab5ec95a7f57ce9e1e7b18ad82b7f9f35958ce8d |
|
MD5 | 10fb526c4cc871a0eadf2d56d90a5328 |
|
BLAKE2b-256 | fabd13bd59049a4576bbcb59dce8553a259d87ce2842dc606ba1f1e9a11d3ea8 |
File details
Details for the file py_redis-1.0.4-py3-none-any.whl
.
File metadata
- Download URL: py_redis-1.0.4-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2315004c333d8b660135d1ac639926ac0d4bc2a50ea399c1aeb09fff3a4b768a |
|
MD5 | 9359e7c0f047e545c9aa6a882226d2af |
|
BLAKE2b-256 | df6e27c69f7345ce9b62566f3c259cf8526caeea2cc659362948c76b797e0215 |