[Python-ideas] [Python-Dev] minmax() function returning (minimum, maximum) tuple of a sequence

Tal Einat taleinat at gmail.com
Tue Oct 12 21:41:00 CEST 2010


Paul Moore wrote:
> On 11 October 2010 21:18, Tal Einat <taleinat at gmail.com> wrote:
>> We recently needed exactly this -- to do several running calculations
>> in parallel on an iterable. We avoided using co-routines and just
>> created a RunningCalc class with a simple interface, and implemented
>> various running calculations as sub-classes, e.g. min, max, average,
>> variance, n-largest. This isn't very fast, but since generating the
>> iterated values is computationally heavy, this is fast enough for our
>> uses.
>>
>> Having a standard method to do this in Python, with implementations
>> for common calculations in the stdlib, would have been nice.
>>
>> I wouldn't mind trying to work up a PEP for this, if there is support
>> for the idea.
>
> The "consumer" interface as described in
> http://effbot.org/zone/consumer.htm sounds about right for this:
>
> class Rmin(object):
>    def __init__(self):
>        self.running_min = None
>    def feed(self, val):
>        if self.running_min is None:
>            self.running_min = val
>        else:
>            self.running_min = min(self.running_min, val)
>    def close(self):
>        pass
>
> rmin = Rmin()
> for val in iter:
>    rmin.feed(val)
> print rmin.running_min

That's what I was thinking about too.

How about something along these lines?
http://pastebin.com/DReBL56T

I just worked that up now and would like some comments and
suggestions. It could either turn into a PEP or an external library,
depending on popularity here.

- Tal Einat



More information about the Python-ideas mailing list