Browse files

simplify code, rename deque methods, 0.3.0

  • Loading branch information...
1 parent 14b1ee7 commit 0834d63314ce114e22fb6165bb0265d34d3ea718 @tnm tnm committed Jul 18, 2011
Showing with 40 additions and 62 deletions.
  1. +39 −61 qr.py
  2. +1 −1 setup.py
View
100 qr.py
@@ -1,17 +1,10 @@
"""
QR | Redis-Based Data Structures in Python
- 16 Dec 2010 | Add more logging (0.2.1)
- 26 Apr 2010 | Major API change; capped collections, remove autopop (0.2.0)
- 7 Mar 2010 | Auto popping for bounded queues is optional (0.1.4)
- 5 Mar 2010 | Returns work for both bounded and unbounded (0.1.3)
- 4 Mar 2010 | Pop commands now return just the value
- 24 Feb 2010 | QR now has deque and stack data structures (0.1.2)
- 22 Feb 2010 | First public release of QR (0.1.1)
"""
__author__ = 'Ted Nyman'
-__version__ = '0.2.1'
+__version__ = '0.3.0'
__license__ = 'MIT'
import redis
@@ -40,42 +33,37 @@ class Deque(object):
def __init__(self, key):
self.key = key
- def pushback(self, element):
- """Push an element to the back"""
- key = self.key
- push_it = redis.lpush(key, element)
- log.debug('Pushed ** %s ** for key ** %s **' % (element, key))
+ def push_back(self, element):
+ """Push an element to the back of the deque"""
+ redis.lpush(self.key, element)
+ log.debug('Pushed ** %s ** for key ** %s **' % (element, self.key))
- def pushfront(self, element):
- """Push an element to the front"""
+ def push_front(self, element):
+ """Push an element to the front of the deque"""
key = self.key
push_it = redis.rpush(key, element)
- log.debug('Pushed ** %s ** for key ** %s **' % (element, key))
+ log.debug('Pushed ** %s ** for key ** %s **' % (element, self.key))
- def popfront(self):
- """Pop an element from the front"""
- key = self.key
- popped = redis.rpop(key)
- log.debug('Popped ** %s ** from key ** %s **' % (popped, key))
+ def pop_front(self):
+ """Pop an element from the front of the deque"""
+ popped = redis.rpop(self.key)
+ log.debug('Popped ** %s ** from key ** %s **' % (popped, self.key))
return popped
- def popback(self):
- """Pop an element from the back"""
- key = self.key
- popped = redis.lpop(key)
- log.debug('Popped ** %s ** from key ** %s **' % (popped, key))
+ def pop_back(self):
+ """Pop an element from the back of the deque"""
+ popped = redis.lpop(self.key)
+ log.debug('Popped ** %s ** from key ** %s **' % (popped, self.key))
return popped
def elements(self):
"""Return all elements as a Python list"""
- key = self.key
- all_elements = redis.lrange(key, 0, -1)
+ all_elements = redis.lrange(self.key, 0, -1)
return all_elements
def elements_as_json(self):
"""Return all elements as a JSON object"""
- key = self.key
- all_elements = redis.lrange(key, 0, -1)
+ all_elements = redis.lrange(self.key, 0, -1)
all_elements_as_json = json.dumps(all_elements)
return all_elements_as_json
@@ -87,61 +75,55 @@ def __init__(self, key):
def push(self, element):
"""Push an element"""
- key = self.key
- push_it = redis.lpush(key, element)
- log.debug('Pushed ** %s ** for key ** %s **' % (element, key))
+ redis.lpush(self.key, element)
+ log.debug('Pushed ** %s ** for key ** %s **' % (element, self.key))
def pop(self):
"""Pop an element"""
- key = self.key
- popped = redis.rpop(key)
- log.debug('Popped ** %s ** from key ** %s **' % (popped, key))
+ popped = redis.rpop(self.key)
+ log.debug('Popped ** %s ** from key ** %s **' % (popped, self.key))
return popped
def elements(self):
"""Return all elements as a Python list"""
- key = self.key
- all_elements = redis.lrange(key, 0, -1) or [ ]
+ all_elements = redis.lrange(self.key, 0, -1) or [ ]
return all_elements
def elements_as_json(self):
"""Return all elements as a JSON object"""
- key = self.key
- all_elements = redis.lrange(key, 0, -1) or [ ]
+ all_elements = redis.lrange(self.key, 0, -1) or [ ]
all_elements_as_json = json.dumps(all_elements)
return all_elements_as_json
class CappedCollection(object):
- """Implements a capped collection (the collection never
- gets larger than the specified size)."""
+ """
+ Implements a capped collection (the collection never
+ gets larger than the specified size).
+ """
def __init__(self, key, size):
self.key = key
self.size = size
def push(self, element):
- key = self.key
size = self.size
pipe = redis.pipeline() # Use multi-exec command via redis-py pipelining
- pipe = pipe.lpush(key, element).ltrim(key, 0, size-1) # ltrim is zero-indexed
+ pipe = pipe.lpush(self.key, element).ltrim(self.key, 0, size-1) # ltrim is zero-indexed
pipe.execute()
def pop(self):
- key = self.key
- popped = redis.rpop(key)
- log.debug('Popped ** %s ** from key ** %s **' % (popped, key))
+ popped = redis.rpop(self.key)
+ log.debug('Popped ** %s ** from key ** %s **' % (popped, self.key))
return popped
def elements(self):
"""Return all elements as Python list"""
- key = self.key
- all_elements = redis.lrange(key, 0, -1)
+ all_elements = redis.lrange(self.key, 0, -1)
return all_elements
def elements_as_json(self):
"""Return all elements as JSON object"""
- key = self.key
- all_elements = redis.lrange(key, 0, -1)
+ all_elements = redis.lrange(self.key, 0, -1)
all_elements_as_json = json.dumps(all_elements)
return all_elements_as_json
@@ -153,26 +135,22 @@ def __init__(self, key):
def push(self, element):
"""Push an element"""
- key = self.key
- push_it = redis.lpush(key, element)
- log.debug('Pushed ** %s ** for key ** %s **' % (element, key))
+ redis.lpush(self.key, element)
+ log.debug('Pushed ** %s ** for key ** %s **' % (element, self.key))
def pop(self):
"""Pop an element"""
- key = self.key
- popped = redis.lpop(key)
- log.debug('Popped ** %s ** from key ** %s **' % (popped, key))
+ popped = redis.lpop(self.key)
+ log.debug('Popped ** %s ** from key ** %s **' % (popped, self.key))
return popped
def elements(self):
"""Return all elements as Python list"""
- key = self.key
- all_elements = redis.lrange(key, 0, -1)
+ all_elements = redis.lrange(self.key, 0, -1)
return all_elements
def elements_as_json(self):
"""Return all elements as JSON object"""
- key = self.key
- all_elements = redis.lrange(key, 0, -1)
+ all_elements = redis.lrange(self.key, 0, -1)
all_elements_as_json = json.dumps(all_elements)
return all_elements_as_json
View
2 setup.py
@@ -4,7 +4,7 @@
import unittest
from setuptools import setup, find_packages
-version = '0.2.1'
+version = '0.3.0'
LONG_DESCRIPTION = '''

0 comments on commit 0834d63

Please sign in to comment.