Skip to content

Instantly share code, notes, and snippets.

@codyhan94
Created November 8, 2014 20:42
Show Gist options
  • Save codyhan94/7b9f00691c855520f51d to your computer and use it in GitHub Desktop.
Save codyhan94/7b9f00691c855520f51d to your computer and use it in GitHub Desktop.
PQ comparable fix
from queue import PriorityQueue
import config
class EventQueue:
def __init__(self):
self.q = PriorityQueue()
def get(self):
if self.q.empty():
return -1
item = self.q.get()
self.q.task_done()
return item
def put(self, event, time):
# Entries are of the form (priority, data)
# Use start time as priority
self.q.put((time, event))
class Person:
def __init__(self, name):
self.name = name
def addToQueue(self, time, item):
config.q.put((time, item))
class Parent(Person):
def __init__(self, name, num_children):
super(Parent, self).__init__(name)
self.num_children = num_children
def __repr__(self):
return (self.name + ' ' + str(self.num_children))
class Child(Person):
def __init__(self, name):
super(Child, self).__init__(name)
class Event(object):
def __init__(self, name):
self.name = name
def __repr__(self):
return self.name
# Hackish way to make Events comparable
def __gt__(self, *args):
return True
def __lt__(self, *args):
return True
class TestEvent(Event):
def __init__(self, name, num):
super().__init__(name)
self.num = num
def main():
config.q = PriorityQueue()
mom = Parent('Diane', 2)
print(mom)
mom.addToQueue(0, 'hello')
child = Child('Cody')
child.addToQueue(3, 'goodbye')
child.addToQueue(2, 'middle')
# Now that Event implements < and >, this can go in PQ that has entries
# (int, string)
config.q.put((0, TestEvent('tester', 9)))
while (not config.q.empty()):
print(config.q.get())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment