libb.ComparableHeap

class ComparableHeap(initial=None, key=<function ComparableHeap.<lambda>>)[source]

Bases: object

Heap with custom key comparator.

Wraps heapq to add a keyed comparator function.

Parameters:
  • initial (list) – Initial items for the heap.

  • key – Key function for comparison (default: identity).

Example:

>>> from datetime import datetime
>>> ch=ComparableHeap(initial=[                {'dtm':datetime(2017,1,1,12,10,59),'val':'one'},                {'dtm':datetime(2017,1,1,12,10,58),'val':'two'}],                key=lambda f: f['dtm'])
>>> ch.pop()
{'dtm': datetime.datetime(2017, 1, 1, 12, 10, 58), 'val': 'two'}
>>> ch.push({'val': 'three', 'dtm': datetime(2017,1,1,12,11,00)})
>>> ch.pop()
{'dtm': datetime.datetime(2017, 1, 1, 12, 10, 59), 'val': 'one'}
push(item)[source]
pop()[source]