[New-bugs-announce] [issue17794] Priority Queue

Carlos Ferreira report at bugs.python.org
Fri Apr 19 02:37:22 CEST 2013


New submission from Carlos Ferreira:

I'm using Priority Queues and followed the Python documentation for a simple example.

>From Queue documentation in Python 3.3
http://docs.python.org/3.3/library/queue.html
"
The lowest valued entries are retrieved first (the lowest valued entry is the one returned by sorted(list(entries))[0]). A typical pattern for entries is a tuple in the form: (priority_number, data).
"

Then I tried this simple code.

>>> pq = PriorityQueue()
>>> pq.put_nowait((0, {'a':1}))
>>> pq.put_nowait((0, {'a':2}))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.3/queue.py", line 187, in put_nowait
return self.put(item, block=False)
File "/usr/lib/python3.3/queue.py", line 146, in put
self._put(item)
File "/usr/lib/python3.3/queue.py", line 230, in _put
heappush(self.queue, item)
TypeError: unorderable types: dict() < dict()

Is this a normal behaviour? I'm not sure if this should be declared as a bug...

Instead of sticking to the first argument that indicates the priority, the heapq algorithm checks the second field and tries to order the dictionaries.

I solved this annoyance by adding a  third field, the object ID. Since the object ID is actually its address in memory, every object will have a different ID. Also, since the queue entries will have the same priority (zero), the id value is used to order the tuples in the heap queue.

>>> pq = PriorityQueue()
>>> a = {'a':1}
>>> b = {'a':2}
>>> pq.put_nowait((0, id(a), a))
>>> pq.put_nowait((0, id(b), b))

----------
components: Interpreter Core
messages: 187321
nosy: Claymore
priority: normal
severity: normal
status: open
title: Priority Queue
type: behavior
versions: Python 3.3

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue17794>
_______________________________________


More information about the New-bugs-announce mailing list