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

Tal Einat taleinat at gmail.com
Thu Oct 14 02:13:05 CEST 2010

On Thu, Oct 14, 2010 at 1:14 AM, Nick Coghlan wrote:
> Why use feed() rather than the existing generator send() API?
> def runningmax(default_max=None):
>    max_value = default_max
>    while 1:
>        value = max(yield max_value)
>        if max_value is None or value > max_value:
>            max_value = value

I tried using generators for this and it came out very clumsy. For one
thing, using generators for this requires first calling next() once to
run the generator up to the first yield, which makes the user-facing
API very confusing. Generators also have to yield a value at every
iteration, which is unnecessary here. Finally, the feedMultiple
optimization is impossible with a generator-based implementation.

> That said, I think this kind of thing requires too many additional
> assumptions about how things are driven to make a particularly good
> candidate for standard library inclusion without use in PyPI library
> first.

I'm not sure. "Rolling your own" for this isn't too difficult, so many
developers will prefer to do so rather then add another dependency
from PyPI. On the other hand, Python's standard library includes
various simple utilities that make relatively simple things easier,
standardized and well tested. Additionally, I think this fits in very
nicely with Python's embracing of iterators, and complements the
itertools library well.

While I'm at it, I'd like to mention that I am aiming at a single very
simple common usage pattern:

from RunningCalc import apply_in_parallel, RunningCount,
RunningNLargest, RunningNSmallest
count, largest10, smallest10 = apply_in_parallel(data, RunningCount(),
RunningNLargest(10), RunningNSmallest(10))

Implementing new running calculation classes would also be very simple.

- Tal Einat

More information about the Python-ideas mailing list