libb.ComparableHeap
- class ComparableHeap(initial=None, key=<function ComparableHeap.<lambda>>)[source]
Bases:
objectHeap 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).
Note
Algorithm from https://stackoverflow.com/questions/8875706/heapq-with-custom-compare-predicate
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'}