Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Alex's approach is great. I won't pretend to compete with his level of expertise, but for sake of completeness, here is another approach using the fantastic <a href="http://docs.python.org/library/queue.html" rel="nofollow noreferrer"><code>Queue.Queue</code></a> class (bonus: thread-safe, but that's kinda useless to you based on your description). This might be easier to understand for you, as you expressed some concern on that point:</p> <h3>myqueue.py</h3> <pre><code>#!/usr/bin/python # Renamed in Python 3.0 try: from Queue import Queue, Full, Empty except: from queue import Queue, Full, Empty from datetime import datetime # Spec 1: Size of each queue should be user-defined. # - maxsize on __init__ # Spec 2: Date an object is added should be recorded. # - datetime.now() is first member of tuple, data is second # Spec 3: I would like to be able to define the order of the items that # belong to each queue. # - Order cannot be rearranged with this queue. # Spec 4: If the queue is full, the addition of an extra item should discard # the oldest item of the queue. # - implemented in put() class MyQueue(Queue): "Wrapper around Queue that discards old items instead of blocking." def __init__(self, maxsize=10): assert type(maxsize) is int, "maxsize should be an integer" Queue.__init__(self, maxsize) def put(self, item): "Put an item into the queue, possibly discarding an old item." try: Queue.put(self, (datetime.now(), item), False) except Full: # If we're full, pop an item off and add on the end. Queue.get(self, False) Queue.put(self, (datetime.now(), item), False) def put_nowait(self, item): "Put an item into the queue, possibly discarding an old item." self.put(item) def get(self): "Get a tuple containing an item and the datetime it was entered." try: return Queue.get(self, False) except Empty: return None def get_nowait(self): "Get a tuple containing an item and the datetime it was entered." return self.get() def main(): "Simple test method, showing at least spec #4 working." queue = MyQueue(5) for i in range(1, 7): queue.put("Test item number %u" % i) while not queue.empty(): time_and_data = queue.get() print "%s =&gt; %s" % time_and_data if __name__ == "__main__": main() </code></pre> <h3>expected output</h3> <pre><code>2009-11-02 23:18:37.518586 =&gt; Test item number 2 2009-11-02 23:18:37.518600 =&gt; Test item number 3 2009-11-02 23:18:37.518612 =&gt; Test item number 4 2009-11-02 23:18:37.518625 =&gt; Test item number 5 2009-11-02 23:18:37.518655 =&gt; Test item number 6 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload