try:
   from heapq import heappush, heappop, heapify
except ImportError:
   # Use our own heapq if we can't find the standard one
   from OurHeapQueue import heappush, heappop, heapify

class PrioQueue:
   """
   A key/value priority queue.
   """

   class Node:
      def __init__(self, value, priority):
         self.value = value
         self.priority = priority
      def __cmp__(self, other):
         return (self.priority - other.priority)

   def __init__(self, items={}):
      self.dict = {}
      self.heap = []
      for (value, priority) in items:
         print("run")
         node = Node(value, priority)
         self.dict[value] = node
         self.heap += node
      heapify(self.heap)

   def __len__(self):
      return len(self.heap)

   def pop(self):
      node = heappop(self.heap)
      del self.dict[node.value]
      return (node.value, node.priority)

   def __getitem__(self, value):
      return self.dict[value]

   def __setitem__(self, value, priority):
      if (self.dict.has_key(value)):
         # Element already exists
         node = self.dict[value]
         if (node.priority < priority):
            # Add the element if it has a lower priority number
            node.priority = priority
            heapify(self.heap)
            return True
         else:
            return False
      else:
         # New element
         node = PrioQueue.Node(value, priority)
         self.dict[value] = node
         heappush(self.heap, node)
         return True



