I want to know if there is any way to check if an object is already in the pririty queue by their __hash__ and __ep__. I have to code in Python 2.7 . Here is an example Code which should print "foo":
import Queue
class Job(object):
def __init__(self, fpriority, spriority, iata , hops, cost):
self.fpriority = fpriority
self.spriority = spriority
self.iata = iata
self.hops = hops
self.cost = cost
def __eq__(self,other):
return (self.fpriority == other.fpriority and self.spriority == other.spriority and self.iata == other.iata)
def __hash__(self):
return hash((self.fpriority , self.spriority , self.iata))
def __cmp__(self, other):
if self.fpriority > other.fpriority:
return 1
elif self.fpriority < other.fpriority:
return -1
else:
if self.spriority > other.spriority:
return 1
elif self.spriority < other.spriority:
return -1
else:
return 0
q = Queue.PriorityQueue()
q.put(Job(153153, 0, 'b', 1 , 1))
q.put(Job(145, 2, 'a', 1 , 1))
k = Job(145, 2, 'a', 1 , 1)
for l in q:
if k == l:
print "foo"