Skip to content

Commit 8ca9b2e

Browse files
committed
Tidies up
1 parent b8a52f0 commit 8ca9b2e

File tree

5 files changed

+44
-40
lines changed

5 files changed

+44
-40
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,26 @@ from rejson import Client, Path
2121
rj = Client(host='localhost', port=6379)
2222

2323
# Set the key `obj` to some object
24-
rj.JSONSet('obj', Path.rootPath(), {
24+
obj = {
2525
'answer': 42,
2626
'arr': [None, True, 3.14],
2727
'truth': {
2828
'coord': 'out there'
2929
}
30-
})
30+
}
31+
rj.JSONSet('obj', Path.rootPath(), obj)
3132

3233
# Get something
33-
question = 'Is there anybody... {}?'.format(
34+
print 'Is there anybody... {}?'.format(
3435
rj.JSONGet('obj', Path('.truth.coord'))
3536
)
3637

37-
# Delete something (or perhaps nothing)
38+
# Delete something (or perhaps nothing), append something and pop it
3839
rj.JSONDel('obj', Path('.arr[0]'))
40+
rj.JSONArrAppend('obj', Path('.arr'), 'something')
41+
print '{} popped!'.format(rj.JSONArrPop('obj', Path('.arr')))
3942

40-
# Update something
43+
# Update something else
4144
rj.JSONSet('obj', Path('.answer'), 2.17)
45+
4246
```

rejson/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
`rejson-py is a client for ReJSON
33
"""
44

5-
from .client import Client
5+
from .client import ReJSONClient
66
from .path import Path

rejson/client.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from sys import stdout
22
import json
33
from redis import StrictRedis, exceptions
4-
from redis._compat import (b, basestring, bytes, imap, iteritems, iterkeys,
5-
itervalues, izip, long, nativestr, unicode,
6-
safe_unicode)
4+
from redis.client import BasePipeline
5+
from redis._compat import (long, nativestr)
76
from .path import Path
87

98
def str_path(p):
@@ -39,7 +38,7 @@ def bulk_of_jsons(b):
3938
b[index] = json.loads(item)
4039
return b
4140

42-
class Client(StrictRedis):
41+
class ReJSONClient(StrictRedis):
4342
"""
4443
Implementation of ReJSON commands
4544
@@ -70,11 +69,12 @@ class Client(StrictRedis):
7069
'JSON.OBJLEN': long_or_none,
7170
}
7271

73-
def __init__(self, **kwargs):
74-
super(Client, self).__init__(**kwargs)
72+
def __init__(self, *args, **kwargs):
73+
super(ReJSONClient, self).__init__(*args, **kwargs)
7574
self.__checkPrerequirements()
76-
# Inject the callbacks for the module's commands
77-
self.response_callbacks.update(self.MODULE_CALLBACKS)
75+
# Set the module commands' callbacks
76+
for k, v in self.MODULE_CALLBACKS.iteritems():
77+
self.set_response_callback(k, v)
7878

7979
def __checkPrerequirements(self):
8080
"Checks that the module is ready"
@@ -237,4 +237,3 @@ def JSONObjLen(self, name, path=Path.rootPath()):
237237
``name``
238238
"""
239239
return self.execute_command('JSON.OBJLEN', name, str_path(path))
240-

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,5 @@
1515
'License :: OSI Approved :: BSD License',
1616
'Programming Language :: Python :: 2.7',
1717
'Topic :: Database',
18-
'Topic :: Software Development :: Testing'
1918
]
2019
)

test/test.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import redis
22
from unittest import TestCase
3-
from rejson import Client, Path
3+
from rejson import ReJSONClient, Path
44

55
class ReJSONTestCase(TestCase):
6-
76
def testJSONSetGetDelShouldSucceed(self):
87
"Test basic JSONSet/Get/Del"
9-
rj = Client()
8+
rj = ReJSONClient()
109
rj.flushdb()
1110

1211
self.assertTrue(rj.JSONSet('foo', Path.rootPath(), 'bar'))
@@ -16,7 +15,7 @@ def testJSONSetGetDelShouldSucceed(self):
1615

1716
def testMGetShouldSucceed(self):
1817
"Test JSONMGet"
19-
rj = Client()
18+
rj = ReJSONClient()
2019
rj.flushdb()
2120

2221
rj.JSONSet('1', Path.rootPath(), 1)
@@ -27,15 +26,15 @@ def testMGetShouldSucceed(self):
2726

2827
def testTypeShouldSucceed(self):
2928
"Test JSONType"
30-
rj = Client()
29+
rj = ReJSONClient()
3130
rj.flushdb()
3231

3332
rj.JSONSet('1', Path.rootPath(), 1)
3433
self.assertEqual('integer', rj.JSONType('1'))
3534

3635
def testNumIncrByShouldSucceed(self):
3736
"Test JSONNumIncrBy"
38-
rj = Client()
37+
rj = ReJSONClient()
3938
rj.flushdb()
4039

4140
rj.JSONSet('num', Path.rootPath(), 1)
@@ -45,7 +44,7 @@ def testNumIncrByShouldSucceed(self):
4544

4645
def testNumMultByShouldSucceed(self):
4746
"Test JSONNumIncrBy"
48-
rj = Client()
47+
rj = ReJSONClient()
4948
rj.flushdb()
5049

5150
rj.JSONSet('num', Path.rootPath(), 1)
@@ -55,7 +54,7 @@ def testNumMultByShouldSucceed(self):
5554

5655
def testStrAppendShouldSucceed(self):
5756
"Test JSONStrAppend"
58-
rj = Client()
57+
rj = ReJSONClient()
5958
rj.flushdb()
6059

6160
rj.JSONSet('str', Path.rootPath(), 'foo')
@@ -64,7 +63,7 @@ def testStrAppendShouldSucceed(self):
6463

6564
def testStrLenShouldSucceed(self):
6665
"Test JSONStrLen"
67-
rj = Client()
66+
rj = ReJSONClient()
6867
rj.flushdb()
6968

7069
rj.JSONSet('str', Path.rootPath(), 'foo')
@@ -74,15 +73,15 @@ def testStrLenShouldSucceed(self):
7473

7574
def testArrAppendShouldSucceed(self):
7675
"Test JSONSArrAppend"
77-
rj = Client()
76+
rj = ReJSONClient()
7877
rj.flushdb()
7978

8079
rj.JSONSet('arr', Path.rootPath(), [1])
8180
self.assertEqual(2, rj.JSONArrAppend('arr', Path.rootPath(), 2))
8281

8382
def testArrIndexShouldSucceed(self):
8483
"Test JSONSArrIndex"
85-
rj = Client()
84+
rj = ReJSONClient()
8685
rj.flushdb()
8786

8887
rj.JSONSet('arr', Path.rootPath(), [0, 1, 2, 3, 4])
@@ -91,7 +90,7 @@ def testArrIndexShouldSucceed(self):
9190

9291
def testArrInsertShouldSucceed(self):
9392
"Test JSONSArrInsert"
94-
rj = Client()
93+
rj = ReJSONClient()
9594
rj.flushdb()
9695

9796
rj.JSONSet('arr', Path.rootPath(), [0, 4])
@@ -100,15 +99,15 @@ def testArrInsertShouldSucceed(self):
10099

101100
def testArrLenShouldSucceed(self):
102101
"Test JSONSArrLen"
103-
rj = Client()
102+
rj = ReJSONClient()
104103
rj.flushdb()
105104

106105
rj.JSONSet('arr', Path.rootPath(), [0, 1, 2, 3, 4])
107106
self.assertEqual(5, rj.JSONArrLen('arr', Path.rootPath()))
108107

109108
def testArrPopShouldSucceed(self):
110109
"Test JSONSArrPop"
111-
rj = Client()
110+
rj = ReJSONClient()
112111
rj.flushdb()
113112

114113
rj.JSONSet('arr', Path.rootPath(), [0, 1, 2, 3, 4])
@@ -120,7 +119,7 @@ def testArrPopShouldSucceed(self):
120119

121120
def testArrTrimShouldSucceed(self):
122121
"Test JSONSArrPop"
123-
rj = Client()
122+
rj = ReJSONClient()
124123
rj.flushdb()
125124

126125
rj.JSONSet('arr', Path.rootPath(), [0, 1, 2, 3, 4])
@@ -129,7 +128,7 @@ def testArrTrimShouldSucceed(self):
129128

130129
def testObjKeysShouldSucceed(self):
131130
"Test JSONSObjKeys"
132-
rj = Client()
131+
rj = ReJSONClient()
133132
rj.flushdb()
134133

135134
obj = { 'foo': 'bar', 'baz': 'qaz' }
@@ -142,37 +141,40 @@ def testObjKeysShouldSucceed(self):
142141

143142
def testObjLenShouldSucceed(self):
144143
"Test JSONSObjLen"
145-
rj = Client()
144+
rj = ReJSONClient()
146145
rj.flushdb()
147146

148147
obj = { 'foo': 'bar', 'baz': 'qaz' }
149148
rj.JSONSet('obj', Path.rootPath(), obj)
150149
self.assertEqual(len(obj), rj.JSONObjLen('obj', Path.rootPath()))
151-
150+
152151
def testUsageExampleShouldSucceed(self):
153152
"Test the usage example"
154153

155154
# Create a new rejson-py client
156-
rj = Client(host='localhost', port=6379)
155+
rj = ReJSONClient(host='localhost', port=6379)
157156

158157
# Set the key `obj` to some object
159-
rj.JSONSet('obj', Path.rootPath(), {
158+
obj = {
160159
'answer': 42,
161160
'arr': [None, True, 3.14],
162161
'truth': {
163162
'coord': 'out there'
164163
}
165-
})
164+
}
165+
rj.JSONSet('obj', Path.rootPath(), obj)
166166

167167
# Get something
168-
question = 'Is there anybody... {}?'.format(
168+
print 'Is there anybody... {}?'.format(
169169
rj.JSONGet('obj', Path('.truth.coord'))
170170
)
171171

172-
# Delete something (or perhaps nothing)
172+
# Delete something (or perhaps nothing), append something and pop it
173173
rj.JSONDel('obj', Path('.arr[0]'))
174+
rj.JSONArrAppend('obj', Path('.arr'), 'something')
175+
print '{} popped!'.format(rj.JSONArrPop('obj', Path('.arr')))
174176

175-
# Update something
177+
# Update something else
176178
rj.JSONSet('obj', Path('.answer'), 2.17)
177179

178180
if __name__ == '__main__':

0 commit comments

Comments
 (0)