How to create object queues in Django?

I am new to Django and I am trying to create a blog myself. I am trying to create a function that I saw implemented in Drupal using the nodequeue module .

What I want to do is create object queues , such as blog post queues. Below I describe how I submit queues for work:

  • the size of each queue must be determined by the user.
  • The date the item was added to the queue must be recorded.
  • I would like to be able to determine the order of the elements belonging to each queue (but I think it will be very difficult).
  • If the queue is full, adding an additional item should discard the oldest item in the queue.

An example of how such a function would be useful is to create a queue of recognized messages .

My current knowledge does not allow me to understand what would be the right way. I would appreciate any pointers.

Thank you in advance

+5
source share
3 answers

Here is one approach:

import collections, datetime, itertools

class nodequeue(object):
  def __init__(self, N):
    self.data = collections.deque(N * [(None, None)])
  def add(self, anobj):
    self.data.popleft()
    self.data.push((anobj, datetime.datetime.now())
  def __iter__(self):
    it = iter(self.data)
    return it.dropwhile(lambda x: x[1] is None, self.data)

This ignores the "ordering" of desires, but it is not so difficult to add, for example:

class nodequeueprio(object):
  def __init__(self, N):
    self.data = collections.deque(N * [(None, None, None)])
  def add(self, anobj, prio):
    self.data.popleft()
    self.data.push((anobj, datetime.datetime.now(), prio)
  def __iter__(self):
    it = iter(self.data)
    return sorted(it.dropwhile(lambda x: x[1] is None, self.data),
                  key=operator.itemgetter(2))

I think pre-populating the queue with a placeholder Nonesimplifies the code because it addcan always discard the leftmost (oldest or None) element before adding a new thing - although it __iter__should then remove the placeholders, it's not so bad.

+5

. , , , , Queue.Queue class (: , ). , :

myqueue.py

#!/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 => %s" % time_and_data


if __name__ == "__main__":
    main()

2009-11-02 23:18:37.518586 => Test item number 2
2009-11-02 23:18:37.518600 => Test item number 3
2009-11-02 23:18:37.518612 => Test item number 4
2009-11-02 23:18:37.518625 => Test item number 5
2009-11-02 23:18:37.518655 => Test item number 6
+3

You can use django activity stream. It does not have an interface, like Nodequeue, but it can be used to create different queues of objects.

0
source

All Articles