Author Claymore
Recipients Claymore
Date 2013-04-19.00:37:21
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1366331842.58.0.144732937336.issue17794@psf.upfronthosting.co.za>
In-reply-to
Content
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))
History
Date User Action Args
2013-04-19 00:37:22Claymoresetrecipients: + Claymore
2013-04-19 00:37:22Claymoresetmessageid: <1366331842.58.0.144732937336.issue17794@psf.upfronthosting.co.za>
2013-04-19 00:37:22Claymorelinkissue17794 messages
2013-04-19 00:37:21Claymorecreate