Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

revised native data type support with cPickle serialization

  • Loading branch information...
commit 81091d3ec30fb9aad53a4082d842feb477da512f 1 parent 18c8725
@wayoutmind wayoutmind authored
Showing with 18 additions and 16 deletions.
  1. +16 −14 qr.py
  2. +2 −2 test/tests.py
View
30 qr.py
@@ -16,6 +16,7 @@
__license__ = 'MIT'
__maintainer__ = 'Fredrick Galoso'
+import cPickle as pickle
import redis
import logging
@@ -25,6 +26,7 @@
import simplejson as json
+
class NullHandler(logging.Handler):
"""A logging handler that discards all logging records"""
def emit(self, record):
@@ -57,13 +59,13 @@ def __init__(self, key, **kwargs):
def pushback(self, element):
"""Push an element to the back"""
key = self.key
- push_it = self.redis.lpush(key, json.dumps(element))
+ push_it = self.redis.lpush(key, pickle.dumps(element))
log.debug('Pushed ** %s ** for key ** %s **' % (element, key))
def pushfront(self, element):
"""Push an element to the front"""
key = self.key
- push_it = self.redis.rpush(key, json.dumps(element))
+ push_it = self.redis.rpush(key, pickle.dumps(element))
log.debug('Pushed ** %s ** for key ** %s **' % (element, key))
def popfront(self):
@@ -72,7 +74,7 @@ def popfront(self):
popped = self.redis.rpop(key)
log.debug('Popped ** %s ** from key ** %s **' % (popped, key))
try:
- return json.loads(popped)
+ return pickle.loads(popped)
except TypeError:
return None
@@ -82,7 +84,7 @@ def popback(self):
popped = self.redis.lpop(key)
log.debug('Popped ** %s ** from key ** %s **' % (popped, key))
try:
- return json.loads(popped)
+ return pickle.loads(popped)
except TypeError:
return None
@@ -95,7 +97,7 @@ def elements(self):
def elements_as_json(self):
"""Return all elements as a JSON object"""
key = self.key
- all_elements = self.redis.lrange(key, 0, -1)
+ all_elements = [pickle.loads(item) for item in self.redis.lrange(key, 0, -1)]
all_elements_as_json = json.dumps(all_elements)
return all_elements_as_json
@@ -110,7 +112,7 @@ def __init__(self, key, **kwargs):
def push(self, element):
"""Push an element"""
key = self.key
- push_it = self.redis.lpush(key, json.dumps(element))
+ push_it = self.redis.lpush(key, pickle.dumps(element))
log.debug('Pushed ** %s ** for key ** %s **' % (element, key))
def pop(self):
@@ -119,7 +121,7 @@ def pop(self):
popped = self.redis.rpop(key)
log.debug('Popped ** %s ** from key ** %s **' % (popped, key))
try:
- return json.loads(popped)
+ return pickle.loads(popped)
except TypeError:
return None
@@ -132,7 +134,7 @@ def elements(self):
def elements_as_json(self):
"""Return all elements as a JSON object"""
key = self.key
- all_elements = self.redis.lrange(key, 0, -1) or []
+ all_elements = [pickle.loads(item) for item in self.redis.lrange(key, 0, -1)] or []
all_elements_as_json = json.dumps(all_elements)
return all_elements_as_json
@@ -150,7 +152,7 @@ def push(self, element):
key = self.key
size = self.size
pipe = self.redis.pipeline() # Use multi-exec command via self.redis-py pipelining
- pipe = pipe.lpush(key, json.dumps(element)).ltrim(key, 0, size - 1) # ltrim is zero-indexed
+ pipe = pipe.lpush(key, pickle.dumps(element)).ltrim(key, 0, size - 1) # ltrim is zero-indexed
pipe.execute()
def pop(self):
@@ -158,7 +160,7 @@ def pop(self):
popped = self.redis.rpop(key)
log.debug('Popped ** %s ** from key ** %s **' % (popped, key))
try:
- return json.loads(popped)
+ return pickle.loads(popped)
except TypeError:
return None
@@ -171,7 +173,7 @@ def elements(self):
def elements_as_json(self):
"""Return all elements as JSON object"""
key = self.key
- all_elements = self.redis.lrange(key, 0, -1)
+ all_elements = [pickle.loads(item) for item in self.redis.lrange(key, 0, -1)]
all_elements_as_json = json.dumps(all_elements)
return all_elements_as_json
@@ -186,7 +188,7 @@ def __init__(self, key, **kwargs):
def push(self, element):
"""Push an element"""
key = self.key
- push_it = self.redis.lpush(key, json.dumps(element))
+ push_it = self.redis.lpush(key, pickle.dumps(element))
log.debug('Pushed ** %s ** for key ** %s **' % (element, key))
def pop(self):
@@ -195,7 +197,7 @@ def pop(self):
popped = self.redis.lpop(key)
log.debug('Popped ** %s ** from key ** %s **' % (popped, key))
try:
- return json.loads(popped)
+ return pickle.loads(popped)
except TypeError:
return None
@@ -208,6 +210,6 @@ def elements(self):
def elements_as_json(self):
"""Return all elements as JSON object"""
key = self.key
- all_elements = self.redis.lrange(key, 0, -1)
+ all_elements = [pickle.loads(item) for item in self.redis.lrange(key, 0, -1)]
all_elements_as_json = json.dumps(all_elements)
return all_elements_as_json
View
4 test/tests.py
@@ -36,7 +36,7 @@ def test_order_mixed(self):
class CappedCollection(unittest.TestCase):
def setUp(self):
r.delete('qrtestcc')
- self.aq = qr.CappedCollection(key='qrtestcc', size=3, host='localhost', port=6379)
+ self.aq = qr.CappedCollection(key='qrtestcc', size=3)
self.assertEquals(len(self.aq.elements()), 0)
def test_roundtrip(self):
@@ -78,7 +78,7 @@ def test_limit(self):
class Stack(unittest.TestCase):
def setUp(self):
r.delete('qrteststack')
- self.stack = qr.Stack(key='qrteststack', host='localhost', port=6379)
+ self.stack = qr.Stack(key='qrteststack')
def test_roundtrip(self):
stack = self.stack
Please sign in to comment.
Something went wrong with that request. Please try again.