bisect and Queue modules in Python 2.4

Discussion in 'Python' started by SA Trygubenko, Mar 16, 2006.

  1. 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/bisect-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
     
    SA Trygubenko, Mar 16, 2006
    #1
    1. Advertising

  2. SA Trygubenko

    Peter Otten Guest

    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/bisect-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
     
    Peter Otten, Mar 16, 2006
    #2
    1. Advertising

  3. Peter Otten <> 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/bisect-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
     
    Alex Martelli, Mar 16, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary Robinson

    bisect uses __cmp__()?

    Gary Robinson, Jul 29, 2003, in forum: Python
    Replies:
    0
    Views:
    341
    Gary Robinson
    Jul 29, 2003
  2. Replies:
    0
    Views:
    335
  3. Russell Warren

    Is Queue.Queue.queue.clear() thread-safe?

    Russell Warren, Jun 22, 2006, in forum: Python
    Replies:
    4
    Views:
    698
    Russell Warren
    Jun 27, 2006
  4. python bisect questions

    , Apr 3, 2008, in forum: Python
    Replies:
    8
    Views:
    1,015
    Gabriel Genellina
    Apr 4, 2008
  5. Kris
    Replies:
    0
    Views:
    503
Loading...

Share This Page