Skip to content

Commit 07ec0ff

Browse files
committed
README rewritten from markdown to rst
1 parent 2c6f8e2 commit 07ec0ff

File tree

3 files changed

+159
-136
lines changed

3 files changed

+159
-136
lines changed

README.md

Lines changed: 0 additions & 135 deletions
This file was deleted.

README.rst

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
ReJSON Python Client
2+
====================
3+
4+
.. image:: https://travis-ci.org/RedisLabs/rejson-py.svg?branch=master
5+
:target: https://travis-ci.org/RedisLabs/rejson-py
6+
7+
.. image:: https://img.shields.io/pypi/pyversions/rejson.svg
8+
:target: https://github.com/RedisLabs/rejson-py
9+
10+
rejson-py is a package that allows storing, updating and querying objects as
11+
JSON documents in a `Redis`_ database that is extended with the
12+
`ReJSON module`_. The package extends
13+
`redis-py`_'s interface with ReJSON's
14+
API, and performs on-the-fly serialization/deserialization of objects to/from
15+
JSON.
16+
17+
.. _`Redis`: https://redis.io
18+
.. _`ReJSON module`: https://github.com/redislabsmodules/rejson
19+
.. _`redis-py`: https://github.com/andymccurdy/redis-py
20+
21+
Installation
22+
------------
23+
24+
.. code-block:: bash
25+
26+
$ pip install rejson
27+
28+
Usage example
29+
-------------
30+
31+
.. code-block:: python
32+
33+
from rejson import Client, Path
34+
35+
rj = Client(host='localhost', port=6379)
36+
37+
# Set the key `obj` to some object
38+
obj = {
39+
'answer': 42,
40+
'arr': [None, True, 3.14],
41+
'truth': {
42+
'coord': 'out there'
43+
}
44+
}
45+
rj.jsonset('obj', Path.rootPath(), obj)
46+
47+
# Get something
48+
print 'Is there anybody... {}?'.format(
49+
rj.jsonget('obj', Path('.truth.coord'))
50+
)
51+
52+
# Delete something (or perhaps nothing), append something and pop it
53+
rj.jsondel('obj', Path('.arr[0]'))
54+
rj.jsonarrappend('obj', Path('.arr'), 'something')
55+
print '{} popped!'.format(rj.jsonarrpop('obj', Path('.arr')))
56+
57+
# Update something else
58+
rj.jsonset('obj', Path('.answer'), 2.17)
59+
60+
# And use just like the regular redis-py client
61+
jp = rj.pipeline()
62+
jp.set('foo', 'bar')
63+
jp.jsonset('baz', Path.rootPath(), 'qaz')
64+
jp.execute()
65+
66+
67+
Encoding/Decoding
68+
-----------------
69+
70+
rejson-py uses Python's json_.
71+
The client can be set to use custom encoders/decoders at creation, or by calling
72+
explicitly the `setEncoder()`_ and
73+
`setDecoder()`_ methods, respectively.
74+
75+
.. _json: https://docs.python.org/2/library/json.html.
76+
.. _`setDecoder()`:API.md#setdecoder
77+
.. _`setEncoder()`:API.md#setencoder
78+
79+
The following shows how to use this for a custom class that's stored as
80+
a JSON string for example:
81+
82+
.. code-block:: python
83+
84+
from json import JSONEncoder, JSONDecoder
85+
from rejson import Client
86+
87+
class CustomClass(object):
88+
"Some non-JSON-serializable"
89+
def __init__(self, s=None):
90+
if s is not None:
91+
# deserialize the instance from the serialization
92+
if s.startswith('CustomClass:'):
93+
...
94+
else:
95+
raise Exception('unknown format')
96+
else:
97+
# initialize the instance
98+
...
99+
100+
def __str__(self):
101+
_str = 'CustomClass:'
102+
# append the instance's state to the serialization
103+
...
104+
return _str
105+
106+
...
107+
108+
class CustomEncoder(JSONEncoder):
109+
"A custom encoder for the custom class"
110+
def default(self, obj):
111+
if isinstance(obj, CustomClass):
112+
return str(obj)
113+
return json.JSONEncoder.encode(self, obj)
114+
115+
class TestDecoder(JSONDecoder):
116+
"A custom decoder for the custom class"
117+
def decode(self, obj):
118+
d = json.JSONDecoder.decode(self, obj)
119+
if isinstance(d, basestring) and d.startswith('CustomClass:'):
120+
return CustomClass(d)
121+
return d
122+
123+
# Create a new instance of CustomClass
124+
obj = CustomClass()
125+
126+
# Create a new client with the custom encoder and decoder
127+
rj = Client(encoder=CustomEncoder(), decoder=CustomDecoder())
128+
129+
# Store the object
130+
rj.jsonset('custom', Path.rootPath(), obj))
131+
132+
# Retrieve it
133+
obj = rj.jsonget('custom', Path.rootPath())
134+
135+
136+
API
137+
---
138+
139+
As rejson-py exposes the same methods as redis-py, it can be used as a drop-in
140+
replacement. On top of Redis' core commands, the client also adds ReJSON's
141+
vocabulary and a couple of helper methods. These are documented in the
142+
[API.md](API.md) file, which can be generated by running:
143+
144+
.. code-block:: bash
145+
146+
$ python gendoc rejson > API.md
147+
148+
149+
For complete documentation about ReJSON's commands, refer to rejson_.
150+
151+
.. _`rejson`: http://rejson.io
152+
153+
License
154+
-------
155+
156+
`BSD 2-Clause`_
157+
158+
.. _`BSD 2-Clause`: https://github.com/RedisLabs/rejson-py/blob/master/LICENSE

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def read_all(f):
3535
setup(name='rejson',
3636
version=version,
3737
description='ReJSON Python Client',
38-
long_description=read_all("README.md"),
38+
long_description=read_all("README.rst"),
3939
classifiers=[
4040
'Programming Language :: Python',
4141
'License :: OSI Approved :: BSD License',

0 commit comments

Comments
 (0)