Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   bisect and Queue modules in Python 2.4 (http://www.velocityreviews.com/forums/t355521-bisect-and-queue-modules-in-python-2-4-a.html)

SA Trygubenko 03-16-2006 02:26 PM

bisect and Queue modules in Python 2.4
 
Dear All,

Python 2.4 (#1, Mar 22 2005, 21:42:42)
[GCC 3.3.5 20050117 (prerelease) (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Queue
>>> q=Queue.Queue()
>>> type(q.queue)

<type 'collections.deque'>
>>>


q.queue used to be a list, and now it is something else?

I was using bisect module to implement min priority queue, as described
in python library reference (see
http://www.python.org/doc/2.3.5/lib/...-example.html). I have found
out that in Python 2.4 q.queue does not support "insert" anymore, which
breaks bisect. Please help!

Thank you,
Semen

Peter Otten 03-16-2006 02:45 PM

Re: bisect and Queue modules in Python 2.4
 
SA Trygubenko wrote:

> q.queue used to be a list, and now it is something else?
>
> I was using bisect module to implement min priority queue, as described
> in python library reference (see
> http://www.python.org/doc/2.3.5/lib/...-example.html). I have found
> out that in Python 2.4 q.queue does not support "insert" anymore, which
> breaks bisect. Please help!


class PriorityQueue(Queue.Queue):
def _put(self, item):
bisect.insort(self.queue, item)
def _init(self, maxsize):
self.maxsize = maxsize
self.queue = []
def _get(self):
return self.queue.pop(0)

or somesuch might work.

Peter

Alex Martelli 03-16-2006 03:33 PM

Re: bisect and Queue modules in Python 2.4
 
Peter Otten <__peter__@web.de> wrote:

> SA Trygubenko wrote:
>
> > q.queue used to be a list, and now it is something else?
> >
> > I was using bisect module to implement min priority queue, as described
> > in python library reference (see
> > http://www.python.org/doc/2.3.5/lib/...-example.html). I have found
> > out that in Python 2.4 q.queue does not support "insert" anymore, which
> > breaks bisect. Please help!

>
> class PriorityQueue(Queue.Queue):
> def _put(self, item):
> bisect.insort(self.queue, item)
> def _init(self, maxsize):
> self.maxsize = maxsize
> self.queue = []
> def _get(self):
> return self.queue.pop(0)
>
> or somesuch might work.


Yep, it should, but using heapq instead of bisect is, I suspect, way
better -- there's a recipe on the Cookbook for that (Queue+heapq), and
though I (of course;-) prefer the version as edited for the printed
(2nd) edition, the online one can also no doubt be useful.


Alex


All times are GMT. The time now is 06:21 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.