473,326 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

A note on heapq module

In few minutes I have just written this quite raw class, it lacks
doctring (the same of the functions of the heapq module), it may
contain bugs still, I haven't tested it much. It's just a simple
wrapper around some of the functions of the heapq module (nsmallest and
nlargest are missing). Usually I use classes only when I need then, I
like functional programming enough, and this class seems to just slow
down the functions of the heapq module. But I think an improved class
similar to this one may be useful (added, replacing isn't necessary)
into the heapq module, because it can avoid cetain errors: I know what
Heaps are and how they work, but some time ago I have written a bug
into small program that uses the functions of the heapq module, because
I have added an item to the head of the heap using a normal list
method, breaking the heap invariant. With a simple class like this one
all the methods of your object keep the heap invariant, avoiding that
kind of bugs. (If you are interested I can improve this class some,
it't not difficult.)
from heapq import heapify, heappush, heappop, heapreplace

class Heap(object):
def __init__(self, init=None):
if init is None:
self.h = []
elif isinstance(init, Heap):
self.h = init.h[:]
else:
self.h = list(init)
heapify(self.h)
def smallest(self):
return self.h[0]
def sort(self, cmp=None, key=None):
self.h.sort(cmp=cmp, key=key)
def heappush(self, item):
heappush(self.h, item)
def heappop(self):
return heappop(self.h)
def heapreplace(self, item):
return heapreplace(self.h, item)
def clear(self):
del self.h[:]
def __contains__(self, item):
return item in self.h
def __hash__(self):
raise TypeError("Heap objects are unhashable.")
def __iter__(self):
return self.h.__iter__()
def __eq__(self, other):
return isinstance(other, Heap) and self.h == other.h
def __ne__(self, other):
return not isinstance(other, Heap) or self.h != other.h
def __len__(self):
return len(self.h)
def __nonzero__(self):
return bool(self.h)
def __str__(self):
return str(self.h)
def __repr__(self):
return "Heap(%s)" % self.h if self.h else "Heap()"

Bye,
bearophile

Jan 16 '07 #1
18 2814
be************@lycos.com wrote:
some time ago I have written a bug
into small program that uses the functions of the heapq module, because
I have added an item to the head of the heap using a normal list
method, breaking the heap invariant.
I know I've had similar bugs in my code before.
from heapq import heapify, heappush, heappop, heapreplace

class Heap(object):
[snip implementation]

+1 for adding a Heap object like this to the collections module. If you
can make a little time to add some tests (you could probably steal a lot
of what you need from test_heapq.py) you should definitely submit it as
a patch for 2.6.

STeVe
Jan 16 '07 #2
I think the sort has to be simplified, otherwise it can't keep the heap
invariant...

def sort(self):
self.h.sort()

Bye,
bearophile

Jan 16 '07 #3
be************@lycos.com kirjoitti:
I think the sort has to be simplified, otherwise it can't keep the heap
invariant...

def sort(self):
self.h.sort()

Bye,
bearophile
And __repr__ should be something like this:
=========
def __repr__(self):
if self.h:
return "Heap(%s)" % self.h
else:
return "Heap()"
==========
to make it compatible with versions earlier than 2.5

Cheers,
Jussi
Jan 16 '07 #4
be************@lycos.com wrote:
In few minutes I have just written this quite raw class, ....
I'd suggest some changes. It is nice to have Heaps with equal
contents equal no matter what order the inserts have been done.
Consider how you want Heap([1, 2, 3]) and Heap([3, 1, 2]) to behave.
Similarly, it is nice to have str and repr produce canonical
representations (I would skip the __str__ code, myself, though).
Also, subclasses should get their identities tweaked as so:
class Heap(object):
...
def sort(self, cmp=None, key=None):
self.h.sort(cmp=cmp, key=key)
I'd remove this method.
...
def __eq__(self, other):
return isinstance(other, Heap) and self.h == other.h
return isinstance(other, self.__class__) and sorted(
self.h) == sorted(other.h)
def __ne__(self, other):
return not isinstance(other, Heap) or self.h != other.h
return not isinstance(other, self.__class__) and sorted(
self.h) != sorted(other.h)
...
def __str__(self):
return str(self.h)
return str(sorted(self.h))
def __repr__(self):
return "Heap(%s)" % self.h if self.h else "Heap()"
return "%s(%s)" % (self.__class__.__name__, sorted(self.h))
And I'd add:
def __lt__(self, other):
raise TypeError('no ordering relation is defined for %s'
% self.__class__.__name__)
__gt__ = __le__ = __ge__ = __lt__

--Scott David Daniels
sc***********@acm.org
Jan 16 '07 #5
Scott David Daniels wrote:

Sorry, I blew the __ne__:
> def __ne__(self, other):
return not isinstance(other, Heap) or self.h != other.h
return not isinstance(other, self.__class__) and sorted(
self.h) != sorted(other.h)
Should be:
return not isinstance(other, self.__class__) or sorted(
self.h) != sorted(other.h)

--Scott David Daniels
sc***********@acm.org
Jan 16 '07 #6
Scott David Daniels:
I'd suggest some changes. It is nice to have Heaps with equal
contents equal no matter what order the inserts have been done.
Consider how you want Heap([1, 2, 3]) and Heap([3, 1, 2]) to behave.
Similarly, it is nice to have str and repr produce canonical
representations (I would skip the __str__ code, myself, though).
Also, subclasses should get their identities tweaked as so:
My version was a *raw* one, just an idea, this is a bit better:
http://rafb.net/p/nLPPjo35.html
I like your changes. In the beginning I didn't want to put __eq__
__ne__ methods at all, because they are too much slow, but being them
O(n ln n) I think your solution is acceptable.

Some methods may have a name different from the heap functions:
def smallest(self):
def push(self, item):
def pop(self):
def replace(self, item):

Two things left I can see: I think the __init__ may have a boolean
inplace parameter to avoid copying the given list, so this class is
about as fast as the original heapify function (but maybe such thing is
too much dirty for a Python stdlib):

def __init__(self, sequence=None, inplace=False):
if sequence is None:
self._heap = []
elif isinstance(sequence, self.__class__):
self._heap = sequence._heap[:]
else:
if inplace and isinstance(sequence, list):
self._heap = sequence
else:
self._heap = list(sequence)
heapify(self._heap)

The second thing, I don't like much the __iter__ because it yields
unsorted items:

def __iter__(self):
return self._heap.__iter__()

Is this good? I think this can be accepted.

Thank you,
bye,
bearophile

Jan 16 '07 #7
On 2007-01-16, be************@lycos.com <be************@lycos.comwrote:
In few minutes I have just written this quite raw class, it lacks
doctring (the same of the functions of the heapq module), it may
contain bugs still, I haven't tested it much. It's just a simple
wrapper around some of the functions of the heapq module (nsmallest and
nlargest are missing). Usually I use classes only when I need then, I
like functional programming enough, and this class seems to just slow
down the functions of the heapq module. But I think an improved class
similar to this one may be useful (added, replacing isn't necessary)
into the heapq module, because it can avoid cetain errors: I know what
Heaps are and how they work, but some time ago I have written a bug
into small program that uses the functions of the heapq module, because
I have added an item to the head of the heap using a normal list
method, breaking the heap invariant. With a simple class like this one
all the methods of your object keep the heap invariant, avoiding that
kind of bugs. (If you are interested I can improve this class some,
it't not difficult.)

[ Heap class based on heapq ]
For me, your class has the same drawback as the heappush, heappop
procedurers: no way to specify a comparision function. Somewhere
in my experimental code I work with line segments in two dimensions.
Now in one place I want from the available segments the one in which the
first point is farthest to the left. In a second place I want from the
available segments the one in which the second point is farthest to the
left. Both can be done with a heap, but currently I can't get both
behaviours while using the same class and using the heapq module or
your Heap class.

--
Antoon Pardon
Jan 17 '07 #8
On 2007-01-16, be************@lycos.com
<be************@lycos.comwrote:
Scott David Daniels:
>I'd suggest some changes. It is nice to have Heaps with equal
contents equal no matter what order the inserts have been
done. Consider how you want Heap([1, 2, 3]) and Heap([3, 1,
2]) to behave. Similarly, it is nice to have str and repr
produce canonical representations (I would skip the __str__
code, myself, though). Also, subclasses should get their
identities tweaked as so:

My version was a *raw* one, just an idea, this is a bit better:
http://rafb.net/p/nLPPjo35.html
I like your changes. In the beginning I didn't want to put
__eq__ __ne__ methods at all, because they are too much slow,
but being them O(n ln n) I think your solution is acceptable.

Some methods may have a name different from the heap functions:
def smallest(self):
def push(self, item):
def pop(self):
def replace(self, item):

Two things left I can see: I think the __init__ may have a
boolean inplace parameter to avoid copying the given list, so
this class is about as fast as the original heapify function
(but maybe such thing is too much dirty for a Python stdlib):
One more idea, cribbed from the linked list thread elsewhere: it
might be nice if your Heap could optionally use an underlying
collections.deque instead of a list.

I don't know how excellent Python's deque is, but it's possible a
deque would provide a faster heap than a contiguous array. C++'s
std::deque is the default implementation of C++'s
std::priority_queue for that reason (unless I'm confused again).

--
Neil Cerutti
We will sell gasoline to anyone in a glass container. --sign at Santa Fe gas
station
Jan 17 '07 #9
Antoon Pardon wrote:
For me, your class has the same drawback as the heappush, heappop
procedurers: no way to specify a comparision function.
Agreed. I'd love to see something like ``Heap(key=my_key_func)``.

STeVe
Jan 17 '07 #10
Steven Bethard:
Antoon Pardon:
For me, your class has the same drawback as the heappush, heappop
procedurers: no way to specify a comparision function.

Agreed. I'd love to see something like ``Heap(key=my_key_func)``.
It can be done, but the code becomes more complex and hairy:
http://rafb.net/p/iCCmDz16.html

(If someone spots a problem please tell me, thank you.)

Bye,
bearophile

Jan 18 '07 #11
Neil Cerutti:
One more idea, cribbed from the linked list thread elsewhere: it
might be nice if your Heap could optionally use an underlying
collections.deque instead of a list.
I don't know how excellent Python's deque is, but it's possible a
deque would provide a faster heap than a contiguous array. C++'s
std::deque is the default implementation of C++'s
std::priority_queue for that reason (unless I'm confused again).
If you have some minutes you can do few speed tests and show us the
code and the timing results...

Bye,
bearophile

Jan 18 '07 #12
On 2007-01-18, be************@lycos.com <be************@lycos.comwrote:
Neil Cerutti:
>One more idea, cribbed from the linked list thread elsewhere:
it might be nice if your Heap could optionally use an
underlying collections.deque instead of a list. I don't know
how excellent Python's deque is, but it's possible a deque
would provide a faster heap than a contiguous array. C++'s
std::deque is the default implementation of C++'s
std::priority_queue for that reason (unless I'm confused
again).

If you have some minutes you can do few speed tests and show us
the code and the timing results...
I'll see what I can come up with. I'll have to test using the
native Python implementation, which should work with deque,
rather than the optimized C implementation, which doesn't.

Unfortunately the inability to take advantage of the C
implementation of heapq might swamp any possible advantage of
using deque instead of list in a Heap class.

--
Neil Cerutti

--
Posted via a free Usenet account from http://www.teranews.com

Jan 18 '07 #13
On 2007-01-18, be************@lycos.com <be************@lycos.comwrote:
Neil Cerutti:
>One more idea, cribbed from the linked list thread elsewhere:
it might be nice if your Heap could optionally use an
underlying collections.deque instead of a list. I don't know
how excellent Python's deque is, but it's possible a deque
would provide a faster heap than a contiguous array. C++'s
std::deque is the default implementation of C++'s
std::priority_queue for that reason (unless I'm confused
again).

If you have some minutes you can do few speed tests and show us
the code and the timing results...
collections.deque is the loser.

Here's the output of the program from my last run:

list: 5.81679827554
deque: 6.40347742423
C heapq: 2.24028186815

Here's the program. You can customize it somewhat to attempt to
model a real program. It builds up a heap of random integers to
some size, performs random pushes and pops for a while, and then
pops the heap down until it's empty.

import random
import timeit

OPCOUNT = 5000
HEAP_SIZE = 100

# Create a random sequence of pushes and pops.
pushes = 0
ops = []
for i in xrange(OPCOUNT):
x = random.randint(0, 1)
if x == 0 or pushes < HEAP_SIZE:
pushes += 1
ops.append(0)
else:
pushes -= 1
ops.append(1)
# Pop off the rest
for i in xrange(pushes):
ops.append(1)

def test(mod, cont):
for op in ops:
if op:
mod.heappop(cont)
else:
mod.heappush(cont, random.randint(0, 150))

# heapqd is the pure Python heapq module without the C implementation.
t1 = timeit.Timer("test(heapqd, list())",
"from __main__ import test; import heapqd")
t2 = timeit.Timer("test(heapqd, deque())",
"from __main__ import test; "\
"from collections import deque; "\
"import heapqd")
t3 = timeit.Timer("test(heapq, list())",
"from __main__ import test; import heapq")
print "list:", t1.timeit(100)
print "deque:", t2.timeit(100)
print "C heapq:", t3.timeit(100)

--
Neil Cerutti
The Pastor would appreciate it if the ladies of the congregation would lend
him their electric girdles for the pancake breakfast next Sunday morning.
--Church Bulletin Blooper

--
Posted via a free Usenet account from http://www.teranews.com

Jan 18 '07 #14
be************@lycos.com wrote:
Steven Bethard:
>Antoon Pardon:
>>For me, your class has the same drawback as the heappush, heappop
procedurers: no way to specify a comparision function.
Agreed. I'd love to see something like ``Heap(key=my_key_func)``.

It can be done, but the code becomes more complex and hairy:
http://rafb.net/p/iCCmDz16.html
Cool!

The current code fails when using unbound methods however::
>>heap = Heap()
Heap.push(heap, 1)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
AttributeError: type object 'Heap' has no attribute 'push'

I would have written the code like::

def smallest(self):
result = self._heap[0]
if self._key is not None:
result = result[2]
return result

def push(self, item):
if self._key is not None:
item = self._key(item), self._itemid, item
self._itemid += 1
heappush(self._heap, item)

def pop(self):
result = heappop(self._heap)
if self._key is not None:
result = result[2]
return result

def popn(self, n):
result = [heappop(self._heap) for _ in xrange(n)]
if self._key is not None:
result = [item[2] for item in result]
return result

def replace(self, item):
if self._key is not None:
item = self._key(item), self._itemid, item
self._itemid += 1
result = heapreplace(self._heap, item)
if self._key is not None:
result = result[2]
return result

This allows unbound methods to work, and substantially lowers the code
duplication. It does add a few extra if statements, but since they're
``is`` tests, they should be pretty fast.

STeVe
Jan 18 '07 #15
Steven Bethard wrote:
The current code fails when using unbound methods however::
I don't like your solution, this class was already slow enough. Don't
use unbound methods with this class :-)
Maybe there's a (better) solution to your problem: to make Heap a
function (or classmethod) that return sone of two possibile objects
created by one of two different classes that have different methods...

Beside that, I think __eq__ method needs more tests, because comparing
a Heap with key against another Heap without key may give some
problems...

I'll think about such problems/things.

Bye,
bearophile

Jan 18 '07 #16
be************@lycos.com wrote:
Steven Bethard wrote:
>The current code fails when using unbound methods however::

I don't like your solution, this class was already slow enough. Don't
use unbound methods with this class :-)
Maybe there's a (better) solution to your problem: to make Heap a
function (or classmethod) that return sone of two possibile objects
created by one of two different classes that have different methods...
That's probably viable. Maybe you should just have two separate
classes: Heap and KeyedHeap. The inplace= parameter doesn't really make
sense for a KeyedHeap anway, so you could just have::

Heap(sequence=None, inplace=False)
KeyedHeap(key, sequence=None)

Of course, this approach ends up with a bunch of code duplication again.

STeVe
Jan 18 '07 #17
Steven Bethard <st************@gmail.comwrites:
Heap(sequence=None, inplace=False)
KeyedHeap(key, sequence=None)
Of course, this approach ends up with a bunch of code duplication again.
Maybe there's a way to use a metaclass that can make either type of
heap but they'd share most methods.
Jan 19 '07 #18
bearophile:
I don't like your solution, this class was already slow enough. Don't
use unbound methods with this class :-)
Sorry for raising this discussion after so much time. Another possibile
solution is to use the normal methods for the normal case, and replace
them only if key is present (this reduces problems with unbound methods
but you may use the wrong method).

Example:

def __init__(self, sequence=None, key=None, inplace=False):
...
if key is None:
...
else:
...
self.pop = self.__pop_key

...
def pop(self):
return heappop(self._heap)
def __pop_key(self):
return heappop(self._heap)[2]
Someone else has done something similar:
http://groups.google.com/group/comp....ba58c9722bf51/

http://sourceforge.net/tracker/index...70&atid=305470

Bye,
bearophile

Jan 26 '07 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Eric | last post by:
I'm missing something critical about how heapq works. I assumed I could iterate through the heap, but I get partial iteration: >>> listB >>> heapify(listB) >>> for h in listB:...
0
by: Stefan Behnel | last post by:
Hi! I filed a patch for a Heap class to be integrated into the heapq module (in addition the the currently available functions). The main features are support for iteration and for the standard...
2
by: Just Me | last post by:
When I print QueryPageSettings gets called a few times before PrintPage is called. I just occurred to me that the problem may be that I do AddHandler each time I print. Is that wrong? If...
3
by: bearophileHUGS | last post by:
I don't like much the syntax of: if __name__ == '__main__': Some time ago I have read this PEP: http://www.python.org/dev/peps/pep-0299/ And why it was refused:...
2
bartonc
by: bartonc | last post by:
>>> import string >>> help(string) Help on module string: NAME string - A collection of string operations (most are no longer used). FILE d:\python25\lib\string.py
2
by: jm.suresh | last post by:
I wanted to have a heap of custom objects, and in different heaps I wanted to have the weights for my elements differently. So, I modified the heapq module to accept key arguments also. The...
1
by: Davy | last post by:
Hi all, I have a dictionary with n elements, and I want to get the m(m<=n) keys with the largest values. For example, I have dic that includes n=4 elements, I want m=2 keys have the largest...
4
by: George Sakkis | last post by:
I spent several hours debugging some bogus data results that turned out to be caused by the fact that heapq.nlargest doesn't respect rich comparisons: import heapq import random class...
7
by: Giampaolo Rodola' | last post by:
Hi, this is related to what I'm trying to implement here: http://groups.google.com/group/comp.lang.python/browse_thread/thread/20796724c1daf1e1# My question is the following: is it safe to avoid...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.