我是 Django 的新手,我正在尝试自己构建一个博客。我正在尝试使用nodequeue模块创建一个我在 Drupal 中实现的功能。
我想要做的是能够创建对象队列,例如博客文章队列。下面,我将描述我如何想象队列的工作原理:
此功能如何有用的一个示例是创建精选帖子队列。
我目前的知识不允许我弄清楚什么是正确的方法。我会很感激任何指针。
提前致谢
答案 0 :(得分:5)
这是一种方法:
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)
这忽略了“排序”的需求,但这不会太难添加,例如:
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))
我认为使用占位符None
预填充队列会简化代码,因为add
总是可以在添加新东西之前删除最左边(最旧或无)的项目 - 即使{{1}然后必须删除占位符,这不是太糟糕。
答案 1 :(得分:3)
Queue.Queue
课程的另一种方法(奖励:线程安全,但根据你的描述,这对你有点无用) 。这对您来说可能更容易理解,因为您对此表示了一些担忧:
#!/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
答案 2 :(得分:0)
您可以使用django-activity-stream。它没有Nodequeue所具有的UI,但它可以用于创建对象的不同队列。