From 5aeec30dba21ecd3286aa3b25193c5e981002a9b Mon Sep 17 00:00:00 2001 From: Mahmoud Hashemi Date: Fri, 22 Mar 2013 19:45:30 -0700 Subject: [PATCH] add sorted queue type, make it the default --- boltons/queueutils.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/boltons/queueutils.py b/boltons/queueutils.py index 67fe689..6ae4b04 100644 --- a/boltons/queueutils.py +++ b/boltons/queueutils.py @@ -12,9 +12,9 @@ except ImportError: try: from listutils import BList - list_type = BList # see BarrelList docstring for notes + # see BarrelList docstring for notes except ImportError: - list_type = list + BList = list __all__ = ['PriorityQueue', 'HeapPriorityQueue', 'SortedPriorityQueue'] @@ -27,10 +27,10 @@ __all__ = ['PriorityQueue', 'HeapPriorityQueue', 'SortedPriorityQueue'] class BasePriorityQueue(object): # negating priority means larger numbers = higher priority _default_priority_key = staticmethod(lambda p: -int(p or 0)) - _list_type = list + _backend_type = list def __init__(self, **kw): - self._pq = self._list_type() + self._pq = self._backend_type() self._entry_map = {} self._counter = itertools.count() self._get_priority = kw.pop('priority_key', self._default_priority_key) @@ -102,10 +102,18 @@ class HeapPriorityQueue(BasePriorityQueue): class SortedPriorityQueue(BasePriorityQueue): - pass + _backend_type = BList + + @staticmethod + def _pop_entry(backend): + return backend.pop(0) + + @staticmethod + def _push_entry(backend, entry): + insort(backend, entry) -PriorityQueue = HeapPriorityQueue +PriorityQueue = SortedPriorityQueue # tests @@ -119,6 +127,13 @@ def main(): assert len(pq) == 1 assert func == pq.pop() assert len(pq) == 0 + try: + pq.pop() + except IndexError: + pass + else: + assert False, 'priority queue should be empty' + import pdb;pdb.set_trace() if __name__ == '__main__': try: