QR helps you create and work with queue, capped collection (bounded queue), deque, and stack data structures for Redis. Redis is well-suited for implementations of these abstract data structures, and QR makes it even easier to work with the structures in Python.
qr.py also creates an instance of the redis-py interface object. You may already have instantiated the object in your code, so you'll want to ensure consistent namespacing. You can remove this line of code, modify the namespacing, or adjust your existing namespacing -- whatever works best for you.
QR as Abstraction
You probably know this already, but here's the 20-second overview of these four lovely data structures.
- You push elements to the back of the queue and pop elements from the front.
- With respect to the elements, it's first in, first out (FIFO).
A capped collection:
- Another name for (what is essentially) a bounded queue.
- You push elements to the back, and once a maximum collection size is reached, the oldest element(s) is trimmed.
A deque, or double-ended queue:
- You can push values to the front or back of a deque, and pop elements from the front or back of the deque.
A stack, or, as they say in German, a 'Stapelspeicher':
- You can push elements to the back of the stack and pop elements from the back of the stack.
- It's last in, first out (LIFO).
Create a QCDS
qr.py includes four little classes: Queue, CappedCollection, Deque, and Stack. To create a new QCDS, just create an instance as follows:
- A first-position key argument is required for all objects. It's the Redis key you want to be associated with the QCDS.
- A second-position size argument is required for CappedCollection. That's how big you want to let the collection get.
Cool, let's create a Beatles queue, circa 1962.
>> from qr import Queue >> bqueue = Queue('Beatles')
You are now the owner of a Queue object (
bqueue), associated with the Redis key 'Beatles'.
>> bqueue.push('Pete') >> bqueue.push('John') >> bqueue.push('Paul') >> bqueue.push('George')
Unfortunately, George Martin doesn't like Pete Best, so it's time to pop him. Since Pete was first in, and this is a queue, after all, we just do this:
>> bqueue.pop() 'Pete'
And, of course, we know we comes in next.
We can return the elements from the queue, too. In fact, each class in QR includes two return-style methods: elements and elements_as_json.
Call elements, and you'll get back a Python list.
Call elements_as_json, and you'll get back the list as a JSON object.
>> bqueue.elements() ['Ringo', 'George', 'Paul', 'John'] >> bqueue.elements_as_json() '['Ringo', 'George', 'Paul', 'John']'
A Capped Collection
I don't know if you've heard, but Donald Knuth will be joining Radiohead soon. They need an organ player. Amazing, I know. Anyway, Radiohead has a max of five members, so someone is going to have to get kicked out of the band. Let's demonstrate this with a Capped Collection.
>> from qr import CappedCollection >> radiohead_cc = Queue('Radiohead', 5) >> radiohead_cc.push('Ed') >> radiohead_cc.push('Colin') >> radiohead_cc.push('Thom') >> radiohead_cc.push('Jonny') >> radiohead_cc.push('Phil') >> radiohead_cc.elements() ['Phil', 'Jonny', 'Thom', 'Colin', 'Ed']
Now it's time for Donald to join the group.
And our new Radiohead is :
>> radiohead_cc.elements() ['Donald', 'Phil', 'Jonny', 'Thom', 'Colin']
If you wanted a deque for the Rolling Stones:
>> from qr import Deque >> stones_deque = Deque('Stones')
The deque, of course, has different methods:
The Kinks stack is as easy as:
>> from qr import Stack >> kinks_stack = Stack('Kinks')
The stack has the same methods as the queue.
Feel free to fork!
Thanks to mafr for some initial tests.
Author: Ted Nyman | @tnm8
Copyright (c) 2010 Ted Nyman
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.