Re: A note on heapq module

2007-01-26 Thread bearophileHUGS
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

Re: A note on heapq module

2007-01-18 Thread bearophileHUGS
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:

Re: A note on heapq module

2007-01-18 Thread bearophileHUGS
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

Re: A note on heapq module

2007-01-18 Thread Neil Cerutti
On 2007-01-18, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: 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

Re: A note on heapq module

2007-01-18 Thread Neil Cerutti
On 2007-01-18, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: 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

Re: A note on heapq module

2007-01-18 Thread Steven Bethard
[EMAIL PROTECTED] 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

Re: A note on heapq module

2007-01-18 Thread bearophileHUGS
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

Re: A note on heapq module

2007-01-18 Thread Steven Bethard
[EMAIL PROTECTED] 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

Re: A note on heapq module

2007-01-18 Thread Paul Rubin
Steven Bethard [EMAIL PROTECTED] writes: 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.

Re: A note on heapq module

2007-01-17 Thread Antoon Pardon
On 2007-01-16, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: 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

Re: A note on heapq module

2007-01-17 Thread Neil Cerutti
On 2007-01-16, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: 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

Re: A note on heapq module

2007-01-17 Thread Steven Bethard
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 -- http://mail.python.org/mailman/listinfo/python-list

A note on heapq module

2007-01-16 Thread bearophileHUGS
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

Re: A note on heapq module

2007-01-16 Thread Steven Bethard
[EMAIL PROTECTED] 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

Re: A note on heapq module

2007-01-16 Thread bearophileHUGS
I think the sort has to be simplified, otherwise it can't keep the heap invariant... def sort(self): self.h.sort() Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: A note on heapq module

2007-01-16 Thread Jussi Salmela
[EMAIL PROTECTED] 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

Re: A note on heapq module

2007-01-16 Thread Scott David Daniels
[EMAIL PROTECTED] 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

Re: A note on heapq module

2007-01-16 Thread Scott David Daniels
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:

Re: A note on heapq module

2007-01-16 Thread bearophileHUGS
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