rejson python客户端

rejson的Python项目详细描述


https://img.shields.io/github/license/RediSearch/redisearch-py.svghttps://travis-ci.org/RedisLabs/rejson-py.svg?branch=masterhttps://badge.fury.io/py/rejson.svghttps://img.shields.io/pypi/pyversions/rejson.svghttps://img.shields.io/github/release/RedisLabs/rejson-py.svghttps://coveralls.io/repos/github/RedisLabs/rejson-py/badge.svg?branch=master

rejson py是一个包,它允许将对象存储、更新和查询为 在Redis数据库中使用 ReJSON module。包扩展了 redis-py与rejson的接口 api,并动态执行对象到/从的序列化/反序列化。 json。

安装

$ pip install rejson

用法示例

fromrejsonimportClient,Pathrj=Client(host='localhost',port=6379)# Set the key `obj` to some objectobj={'answer':42,'arr':[None,True,3.14],'truth':{'coord':'out there'}}rj.jsonset('obj',Path.rootPath(),obj)# Get somethingprint'Is there anybody... {}?'.format(rj.jsonget('obj',Path('.truth.coord')))# Delete something (or perhaps nothing), append something and pop itrj.jsondel('obj',Path('.arr[0]'))rj.jsonarrappend('obj',Path('.arr'),'something')print'{} popped!'.format(rj.jsonarrpop('obj',Path('.arr')))# Update something elserj.jsonset('obj',Path('.answer'),2.17)# And use just like the regular redis-py clientjp=rj.pipeline()jp.set('foo','bar')jp.jsonset('baz',Path.rootPath(),'qaz')jp.execute()

编码/解码

rejson py使用python的json。 客户端可以设置为在创建时使用自定义编码器/解码器,或者通过调用 显式地setEncoder()和 ^分别是{a12}()方法。

下面显示如何将其用于存储为 json字符串,例如:

fromjsonimportJSONEncoder,JSONDecoderfromrejsonimportClientclassCustomClass(object):"Some non-JSON-serializable"def__init__(self,s=None):ifsisnotNone:# deserialize the instance from the serializationifs.startswith('CustomClass:'):...else:raiseException('unknown format')else:# initialize the instance...def__str__(self):_str='CustomClass:'# append the instance's state to the serialization...return_str...classCustomEncoder(JSONEncoder):"A custom encoder for the custom class"defdefault(self,obj):ifisinstance(obj,CustomClass):returnstr(obj)returnjson.JSONEncoder.encode(self,obj)classTestDecoder(JSONDecoder):"A custom decoder for the custom class"defdecode(self,obj):d=json.JSONDecoder.decode(self,obj)ifisinstance(d,basestring)andd.startswith('CustomClass:'):returnCustomClass(d)returnd# Create a new instance of CustomClassobj=CustomClass()# Create a new client with the custom encoder and decoderrj=Client(encoder=CustomEncoder(),decoder=CustomDecoder())# Store the objectrj.jsonset('custom',Path.rootPath(),obj))# Retrieve itobj=rj.jsonget('custom',Path.rootPath())

API

由于rejson py公开了与redis py相同的方法,因此可以将其用作 更换。在redis的核心命令之上,客户端还添加了rejson的 词汇表和一些辅助方法。这些记录在 [api.md](api.md)文件,可通过运行生成:

$ python gendoc rejson > API.md

有关rejson命令的完整文档,请参阅ReJSON’s website

许可证

BSD 2-Clause

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java操作数组上的字符串   java JAXB内容未实例化   图形在Java中如何绘制垂直居中的字符串?   java Apache Ant:使用Junit时出现NoClassDefFoundError   java无法从服务器上运行perl脚本   如何在java中沿树进行预排序遍历,并打印0和1以对应每个节点上的特定字符?   java如何创建。p12文件?   java线程访问无效   java只匹配命名空间中的XML节点,而不知道NS前缀   从java获取2d arraylist元素   数组Java动态集合对象   java Xpath通过通配符或布尔运算查找以相同名称开头的节点?   java注释元素类型   java在中看不到Super()。反编译后的类文件