Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to heapify by field from custom objects

I am receiving on server objects from clients ( every object has same structure and have field self.utc_time which contains time of creation of that object). I need to store in some structure so I always have sorted in ascending so when I pop I pop the oldest object by utc_time, not by time when I receive. I thought to use priority queue from heapq but how to say to heaify by utc_time field from custom objects ? Is there better solution ?

like image 791
Damir Avatar asked Aug 16 '12 14:08

Damir


2 Answers

Add the magic __cmp__ comparison method to your class to avoid needing to do the tuple-decoration that Maksim describes:

>>> import heapq
>>> class MyObject(object):
...     def __init__(self, val):
...         self.val = val
...     def __cmp__(self, other):
...         return cmp(self.val, other.val)
...         
...     
... 
>>> q = []
>>> heapq.heappush(q, MyObject(50))
>>> heapq.heappush(q, MyObject(40))
>>> heapq.heappush(q, MyObject(30))
>>> heapq.heappush(q, MyObject(20))
>>> heapq.heappush(q, MyObject(200))
>>> obj = heapq.heappop(q)
>>> print obj.val
20

Note: Override __lt__ in Python 3, __cmp__ only in Python 2

like image 57
bgporter Avatar answered Sep 27 '22 16:09

bgporter


Python documentation implicitly provides the following solution:

Heap elements can be tuples. This is useful for assigning comparison values (such as task priorities) alongside the main record being tracked:

 h = []
 heappush(h, (5, 'write code'))
 heappush(h, (7, 'release product'))
 heappush(h, (1, 'write spec'))
 heappush(h, (3, 'create tests'))
 heappop(h)
   => (1, 'write spec')

You can do it in a similar fashion - store tuples where the first element will contain a utc_time of an object being placed to the PQ and the second - a reference to object itself.

In a similar SO question it's proposed to create an easy-to-use wrapper that would allow a cleaner way of working with a priority queue.

like image 45
Maksim Skurydzin Avatar answered Sep 27 '22 18:09

Maksim Skurydzin