[PYTHON] I wrote gxredis to use redis-py safely

Overview

Python has a handy redis library called redis-py, but it was difficult to use safely, so it's simple and intuitive. I wrote a wrapper library.

Installation method

$ pip install gxredis

Design concept

――Do not force Active Record specifications, but take advantage of the characteristics of redis obediently --Make the configuration less likely to cause erroneous operations --Use the methods provided by redis-py as they are

How to use

Definition of DAO

For DAO, specify the format and type of key.

import redis
from gxredis import *

class ItemDao(RedisDao):
       item = RedisString("device:{device_id}:item:{item_id}")
       item_list = RedisList("device:{device_id}:list")
       item_set = RedisSet("device:{device_id}:set")
       item_hash = RedisHash("device:{device_id}:hash")
       item_zset = RedisSortedSet("device:{device_id}:zset")

DAO initialization

Pass the redis-py StrictRedis as the first argument. In the second argument, pass the parameters to configure the key. You only need to pass the parameters that are fixed at the time of DAO generation, as you can fill in the missing parameters later.

client = redis.StrictRedis("localhost", 6379, 15)
dao = ItemDao(client, key_params={"device_id": "GX123"})

DAO attributes

The DAO attribute is an accessor to access redis.

>>> dao.item
RedisString(key="device:{device_id}:item:{item_id}", key_params={'device_id': 'GX123'})

>>> dao.item_list
RedisList(key="device:{device_id}:list", key_params={'device_id': 'GX123'})

Basic usage

You can perform operations on the accessor that match the type. Since the key corresponding to the accessor is used, specify the second and subsequent arguments of the redis command.

>>> dao.item_list.lpush("a")
>>> dao.item_list.lpush("b")
>>> dao.item_list.lpush("c")
>>> dao.item_list.lrange(0, 3)
['c', 'b', 'a']

If you don't provide enough parameters for the key, you'll get an exception.

>>> dao.item.get()
...
AttributeError: Not enough keys are provided for redis operation

Complement parameters

By passing additional parameters to the accessor and executing it, you get a new accessor with complemented parameters.

>>> dao.item(item_id=1)
RedisString(key="device:{device_id}:item:{item_id}", key_params={'item_id': 1, 'device_id': 'GX123'})

You can issue the redis command to the newly generated accessor.

>>> accr = dao.item(item_id=1)
>>> accr.set("abc")
>>> accr.get()
'abc'

Use pipeline

A pipeline is also available.

>>> pipe = dao.pipeline()
>>> accr1 = pipe.item(item_id=1)     # accessor for item01
>>> accr2 = pipe.item(item_id=2)     # accessor for item02
>>> accr1.set("item01")
>>> accr2.set("item02")
>>> pipe.item_list.rpush(accr1.key)
>>> pipe.item_list.rpush(accr2.key)
>>> pipe.execute()
>>> dao.item_list.lrange(0, 100)
['device:GX123:item:1', 'device:GX123:item:2',]

Use JSON

Some convenient functions are provided for input / output in JSON.

>>> dao.item(item_id=1).set_json({'hello': 'world'})
>>> dao.item(item_id=1).get_json()
{u'hello': u'world'}

MGET with the key stored in SET or LIST

This also has a convenient method.

>>> dao.item_list.lrange_mget(0, 100)
({'device:GX123:item:1', 'device:GX123:item:2'}, ['{"hello": "world"}', 'item02'])
>>> dao.item_set.smembers_mget_json(0, 0)
(['device:GX123:item:1'], [{u'hello': u'world'}])

You can also use smembers_mget, members_mget_json.

Summary

We have implemented a light wrapper library for safe use of redis-py. The number of lines of code is short, so please read it if you like.

We plan to add key validation functions in the future.

~~ Oh, I have to register with pypi before that. ~~

PyPi Registration Done!

Recommended Posts

I wrote gxredis to use redis-py safely
How to use redis-py Dictionaries
I want to use jar from python
I want to use Linux on mac
I want to use IPython Qt Console
I tried to use lightGBM, xgboost with Boruta
I wrote matplotlib
I wrote "Introduction to Effect Verification" in Python
I want to use Temporary Directory with Python2
I want to use ceres solver from python
I don't want to use -inf with np.log
I wrote a script to upload a WordPress plugin
I want to use ip vrf with SONiC
I tried to summarize how to use matplotlib of python
I want to use self in Backpropagation (tf.custom_gradient) (tensorflow)
I wanted to use the Python library from MATLAB
I tried to summarize how to use pandas in python
I want to use OpenJDK 11 on Ubuntu Linux 18.04 LTS / 18.10
After updating to Catalina, I couldn't use anaconda anymore ...
I want to use the R dataset in python
I wrote you to watch the signal with Go
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use shogun
How to use Pandas 2
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
I started to analyze
Easy to use Flask
How to use partial
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use IPython
How to use virtualenv
I tried to debug.
How to use Matplotlib
How to use numpy
Reasons to use logarithm
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
I tried to paste
How to use Pyenv
Easy to use SQLite3
How to use list []
How to use python-kabusapi
Python-How to use pyinstaller
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
I want to use the latest gcc without sudo privileges! !!
I wrote a program quickly to study DI with Python ①
I want to use only the normalization process of SudachiPy