[issue44571] itertools: takedowhile()

2021-10-11 Thread paul rubin
paul rubin added the comment: Bah, the above doesn't work in the cases where the iterator is empty or (different symptom) where the tail part is empty. Rather than posting a corrected version (unless someone wants it) I'll just say not to use that code fragment, but that the intended API st

[issue44571] itertools: takedowhile()

2021-10-11 Thread paul rubin
paul rubin added the comment: Oh wow, before_and_after will go into the itertools module per that patch? I found this issue while looking for a way to this, but had written the following implementation: def span(pred, xs): # split xs into two iterators a,b where a() is the prefix of xs

[issue44571] itertools: takedowhile()

2021-09-04 Thread miss-islington
miss-islington added the comment: New changeset 656b0bdfaae3a36d386afe3f7b991744528c3ff7 by Miss Islington (bot) in branch '3.10': bpo-44571: Add itertool recipe for a variant of takewhile() (GH-28167) https://github.com/python/cpython/commit/656b0bdfaae3a36d386afe3f7b991744528c3ff7 --

[issue44571] itertools: takedowhile()

2021-09-04 Thread Raymond Hettinger
Change by Raymond Hettinger : -- resolution: -> fixed stage: patch review -> resolved status: open -> closed ___ Python tracker ___ ___

[issue44571] itertools: takedowhile()

2021-09-04 Thread miss-islington
Change by miss-islington : -- nosy: +miss-islington nosy_count: 4.0 -> 5.0 pull_requests: +26600 pull_request: https://github.com/python/cpython/pull/28173 ___ Python tracker _

[issue44571] itertools: takedowhile()

2021-09-04 Thread Raymond Hettinger
Raymond Hettinger added the comment: New changeset 91be41ad933e24bff26353a19f56447e17fb6367 by Raymond Hettinger in branch 'main': bpo-44571: Add itertool recipe for a variant of takewhile() (GH-28167) https://github.com/python/cpython/commit/91be41ad933e24bff26353a19f56447e17fb6367 --

[issue44571] itertools: takedowhile()

2021-09-04 Thread Raymond Hettinger
Change by Raymond Hettinger : -- keywords: +patch pull_requests: +26596 stage: -> patch review pull_request: https://github.com/python/cpython/pull/28167 ___ Python tracker __

[issue44571] itertools: takedowhile()

2021-07-12 Thread pavel-lexyr
pavel-lexyr added the comment: Thank you - that answers the questions. The use case where we would want to know if the last element is transitional or not completely slipped my mind for some reason. -- ___ Python tracker

[issue44571] itertools: takedowhile()

2021-07-12 Thread Tim Peters
Tim Peters added the comment: That said, if you really do want those semantics, it's easy to build on top of Raymond's API: def takewhile_plus_one_more_if_any(pred, iterable): from itertools import islice, chain before, after = before_and_after(pred, iterable) return chain(before,

[issue44571] itertools: takedowhile()

2021-07-12 Thread Tim Peters
Tim Peters added the comment: If you don't use the 'after` iterator, then of course you'll never see the values (if any) it would have yielded. How could it possibly be otherwise? By design and construction, the `before` iterator ends before yielding the first (if any) transitional element.

[issue44571] itertools: takedowhile()

2021-07-12 Thread pavel-lexyr
pavel-lexyr added the comment: There is a core part of the `takedowhile` proposal's use case that I am having trouble envisioning via the alternative `before_and_after` proposal. If the `after` part of the iterator the user does not engage with, the transitional elements will be stuck indefi

[issue44571] itertools: takedowhile()

2021-07-10 Thread Tim Peters
Tim Peters added the comment: I agree Raymond's `before_and_after()` looks like an elegant, efficient, and usable approach to this. One minor nit: there's no need for the `iter()` call in: yield from iter(transition) Indeed, it confused me at first, because `yield from x` does its o

[issue44571] itertools: takedowhile()

2021-07-09 Thread Raymond Hettinger
Raymond Hettinger added the comment: > For convenience, the takewhile iterator can also have > additional attributes: a boolean attribute which indicates > that the falsifying element is set, and dynamic attribute > which is equal to orig_iterator > or chain([odd_element], orig_iterator). R

[issue44571] itertools: takedowhile()

2021-07-09 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: For convenience, the takewhile iterator can also have additional attributes: a boolean attribute which indicates that the falsifying element is set, and dynamic attribute which is equal to orig_iterator or chain([odd_element], orig_iterator). --

[issue44571] itertools: takedowhile()

2021-07-09 Thread Raymond Hettinger
Raymond Hettinger added the comment: I've done some API experiments using a data munging example. See attached file. The proposed API for takewhile() to save the last attribute is somewhat awkward to use: it = iter(report) tw_it = takewhile(is_header, it) for line in takewhile(i

[issue44571] itertools: takedowhile()

2021-07-08 Thread Raymond Hettinger
Raymond Hettinger added the comment: > What if set the last item as an attribute of the takewhile iterator? Perhaps raise an attribute error unless the falsifying element is set? -- ___ Python tracker _

[issue44571] itertools: takedowhile()

2021-07-06 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: What if set the last item as an attribute of the takewhile iterator? -- nosy: +serhiy.storchaka ___ Python tracker ___

[issue44571] itertools: takedowhile()

2021-07-05 Thread pavel-lexyr
pavel-lexyr added the comment: I see. If the syntax allows for better ways to do it now, perhaps a move towards deprecation would be a better idea then? This would agree with the Zen. Also, please elaborate more on the generator-based solutions you have in mind. The suggestion stems from a v

[issue44571] itertools: takedowhile()

2021-07-05 Thread Raymond Hettinger
Change by Raymond Hettinger : -- components: +Library (Lib) versions: +Python 3.11 ___ Python tracker ___ ___ Python-bugs-list maili

[issue44571] itertools: takedowhile()

2021-07-05 Thread Raymond Hettinger
Raymond Hettinger added the comment: Thanks for the suggestion. I agree that the loss of the non-matching element is an irritant. The suggestion to return the first false element would solve that problem but is itself hard to work with. The result would be difficult to reason about becaus

[issue44571] itertools: takedowhile()

2021-07-05 Thread pavel-lexyr
New submission from pavel-lexyr : As described in the documentation, itertools.takewhile() returns all the elements until the first one that does not match the provided criterion. In case of a destructive iterator, or one with side effects, not yielding an element downstream may render takewh