Search code examples
pythonpython-3.xpriority-queuetypingpython-dataclasses

Priority queue with custom type class gets TypeError("'>' not supported between instances of 'PrioritizedItem' and 'int'",)?


I am doing A* search and need to store information in priority queue. But I want the priority queue to compare only one field of my data. So I designed a classPrioritizedItem for priori queue. However, my program keeps running forever. I wrote some simple test code to reproduce this error:

from dataclasses import dataclass,field
import queue

@dataclass(order=True)
class PrioritizedItem:
    hint: float
    current: tuple=field(compare=False)
    path: list=field(compare= False)
trace=[(0, 0)]
temp=PrioritizedItem(hint=14, current=(0, 0) ,path=trace) 
priori_queue = queue.PriorityQueue(temp ) #queue contains current location
priori_queue

enter image description here

After initialization, my priority queue prior_queue is still empty. Manually add the element temp even gives me an error TypeError("'>' not supported between instances of 'PrioritizedItem' and 'int'",). Looks like my type class PriortizedItem is missing some piece. How to add that part?


Solution

  • You're passing temp in PriorityQueue(temp), but the only argument to PriorityQueue is maxsize, which should be an integer. The default is 0, which means the queue grows with no size limit.

    priori_queue = queue.PriorityQueue()
    priori_queue.put(temp)