QR from Ted Nyman makes it simple to create double ended queues, queues, and stacks in Redis from Python. Double ended queues allow items to be added to the beginning or end of the queue while normal queues and stacks are first-in-first-out (FIFO) and last-in-first-out (LIFO) respectively.
To create a queue simply import QR:
>> from qr import Queue
and create the queue:
>> bqueue = Queue('Beatles', 3)
Here we created a regular Queue, but you can also create a double ended queue with
Dequeue or stack with
To added data, use
>> bqueue.push('Ringo') PUSHED: 'Ringo' >> bqueue.push('Paul') PUSHED: 'Paul' >> bqueue.push('John') PUSHED: 'John' >> bqueue.push('George') PUSHED: 'George' 'Ringo'
To grab the next item simply call
>> bqueue.pop() 'Paul'
You can also grab all items in the queue, even as JSON:
>> beatles_queue.elements() ['Ringo', 'John', 'George'] >> beatles_queue.elements_as_json() '['Ringo', 'John', 'George']'
Another cool feature of QR is that all three queue varieties may be set up as bounded or unbounded:
- Bounded: once the DQS reaches a specified size of elements, it will pop an element.
Unbounded: the DQS can grow to any size, and will not pop elements unless you explicitly ask it to.
Have comments? Send a tweet to @TheChangelog on Twitter.
Subscribe to The Changelog Weekly – our weekly email covering everything that hits our open source radar.