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如何从存储在ArrayList<Node>中的动态生成的文本字段中获取文本?   java如何立即关闭InputStream?   如何重新启动Java程序以激活环境变量   java搜索字符串是否相差一个字符   java CFB模式输出与CTR输出相同;我做错什么了吗?   java如何在javaFX中将实例化对象添加到Stage   java如何在jtextarea上打印来自不同类的文本消息   java以编程方式确定IOException的原因?   限制Java NIO通道(文件或socket)中的可用内容   javajaxb与JDOM:是否可以使用JAXB更新xml文件   批处理文件到java测试   JavaFX:stage的作用是什么。可设置大小(false)是否会导致额外的页边距?   java有没有办法告诉IntelliJ按需堆叠参数?   java Seam会话范围的组件在下一个请求中消失   java Google Web Toolkit对开发复杂的java脚本有用吗?   安卓 studio java ArrayList正在检索最高值   java为什么递归地用随机数填充LinkedList时会出现StackOverflowException?