Raymond Hettinger wrote: >> Aahz <[EMAIL PROTECTED]> wrote: >>>> Some open question remain: >>>> - should iwindow return lists or tuples? >>>> - what happens if the length of the iterable is smaller than the >>>> window size, and no padding is specified? Is this an error? Should >>>> the generator return no value at all or one window that is too small? > > > An itertools windowing function was previously discussed and rejected.
A python-dev Google search for "itertools window" found me your original suggestion to include Jack Diedrich's itertools.window in Python 2.3 (which was only deferred because 2.3 was already past beta 1 at that point). I couldn't find any discussion of the idea after that (aside from your pointing out that you'd never found a real-life use case for the pairwise() recipe in the docs, which is a basic form of windowing). One option would be to add a windowing function to the recipes in the itertools docs. Something like: def window(iterable, window_len=2, window_step=1): iterators = tee(iterable, window_len) for skip_steps, itr in enumerate(iterators): for ignored in islice(itr, skip_steps): pass window_itr = izip(*iterators) if window_step != 1: window_itr = islice(window_itr, step=window_step) return window_itr As you can see, I'd leave the padding out of this function signature - if you want padding you can more easily specify it directly when calculating the iterable to be windowed. Do you want padding before and after the sequence, or just after? Pad with None or with zeroes? Better to expand on the "padnone" recipe: def pad(iterable, pad_value=None, times=None): """Pads an iterable with a repeating value The quantity of padding may be limited by specifying a specific number of occurrences. """ return chain(iterable, repeat(pad_value, times)) def padwindow(iterable, window_len, pad_value=None): """Pads an iterable appropriately for the given window length Padding is added to both the front and back of the sequence such that the first and last windows will each contain one value from the actual sequence. """ return chain(repeat(pad_value, window_len-1), iterable, repeat(pad_value, window_len-1)) Cheers, Nick. -- Nick Coghlan | [EMAIL PROTECTED] | Brisbane, Australia --------------------------------------------------------------- http://www.boredomandlaziness.org _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com