README.md

QR

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.

Quick Setup

Redis is available in many package managers by default, or built from source.
redis-py is available via setuptools or pip:

sudo pip install redis

Then install qr:

python setup.py install

Basics of QR

QR queues store serialized Python objects (using cPickle by default), but that can be changed by
setting the serializer on a per-queue basis. This means "Python object in, and Python object out." There are a few constraints
on what can be pickled, and thus put into queues:

You probably know this already, but here's the 20-second overview of these data structures.

A queue:

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).

A priority queue

Push elements into a priority queue with scores, and then retrieve the elements in order of their respective scores.

Values stored in the priority queue are unique.

Using QR

QR contains a few small classes to represent each data structure. To get access to one of the data structures, you create an instance. You can pass custom options for the underlying Redis instance as keyword arguments. For example:

Queue('brand_new_queue_name', host='localhost', port=9000)

A first-position key argument is required for all objects. It's the string name of the Redis key you want to be associated with the new data structure.

A Queue

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 underlying Redis key 'Beatles'.

A Deque

A Stack

The Kinks stack is:

>> from qr import Stack
>> kinks_stack = Stack('Kinks')

The stack has the same methods as the queue.

A Priority Queue

Suppose you want to process various tasks in an order other than you received them. Instead you can
base this processing on a score associated with each task. Maybe you want to process bands in the order of how
many fans they have:

It's important to note that items in the queue are sorted by a score in ascending order, meaning
that the items with the least score is popped off first. Additionally, values stored in the priority
queue are unique. So, if you insert the same value twice with different scores, the value will only
appear once in the queue, with the second score provided:

All Queue Types

You can also get the number of elements in the queue like you would from any normal Python list:

>> len(q)
3

You can also look up a particular element from the queue (or range of elements). Note carefully: in Redis, lists are linked lists, and so index lookups are O(n) to lookup the n'th position. Although this functionality available in qr, you should be careful looking up large indices. Looking at the front or back of the queue is cheap, though:

Additions, More

Thanks to mafr for initial tests and dlecocq/seomoz for serialization work.

Author: Ted Nyman | @tnm

MIT License

Copyright (c) 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.