mirror of https://github.com/scrapy/scrapy.git
added __len__ to PriorityQueue/Stack, and changed __iter__ implementation to return (item, priority) tuples, added more test cases
--HG-- extra : convert_revision : svn%3Ab85faa78-f9eb-468e-a121-7cced6da292c%40713
This commit is contained in:
parent
2434000cda
commit
ff637d9a0a
|
|
@ -10,11 +10,15 @@ class DatatypesTestCase(unittest.TestCase):
|
|||
output = [('one', 1), ('three-1', 3), ('three-2', 3), ('five', 5), ('six', 6)]
|
||||
|
||||
pq = PriorityQueue()
|
||||
assert not bool(pq)
|
||||
for item, prio in input:
|
||||
pq.push(item, prio)
|
||||
out = []
|
||||
assert bool(pq)
|
||||
self.assertEqual(len(pq), len(input))
|
||||
while pq:
|
||||
out.append(pq.pop())
|
||||
self.assertEqual(len(pq), 0)
|
||||
self.assertEqual(out, output)
|
||||
|
||||
def test_priority_stack(self):
|
||||
|
|
@ -23,11 +27,15 @@ class DatatypesTestCase(unittest.TestCase):
|
|||
output = [('one', 1), ('three-2', 3), ('three-1', 3), ('five', 5), ('six', 6)]
|
||||
|
||||
pq = PriorityStack()
|
||||
assert not bool(pq)
|
||||
for item, prio in input:
|
||||
pq.push(item, prio)
|
||||
assert bool(pq)
|
||||
out = []
|
||||
self.assertEqual(len(pq), len(input))
|
||||
while pq:
|
||||
out.append(pq.pop())
|
||||
self.assertEqual(len(pq), 0)
|
||||
self.assertEqual(out, output)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
|
|
@ -420,8 +420,11 @@ class PriorityQueue(object):
|
|||
priority, _, item = heappop(self.items)
|
||||
return item, priority
|
||||
|
||||
def __len__(self):
|
||||
return len(self.items)
|
||||
|
||||
def __iter__(self):
|
||||
return ((priority, item) for priority, _, item in self.items)
|
||||
return ((item, priority) for priority, _, item in self.items)
|
||||
|
||||
def __nonzero__(self):
|
||||
return bool(self.items)
|
||||
|
|
|
|||
Loading…
Reference in New Issue