Re: [Python-ideas] Add single() to itertools

2017-11-02 Thread Neil Girdhar
This request is called "one" in more-itertools: http://more-itertools.readthedocs.io/en/latest/api.html It raises ValueError as Steve suggested. On Monday, October 30, 2017 at 8:34:26 AM UTC-6, Guido van Rossum wrote: > > This is a key example of a case where code speaks. Can you write an > imp

Re: [Python-ideas] Add single() to itertools

2017-10-31 Thread Cameron Simpson
On 31Oct2017 22:50, Greg Ewing wrote: Koos Zevenhoven wrote: |defsingle(i): try: ||v =i.next() |||exceptStopIteration:raiseException('No values')|||try: ||i.next() ||exceptStopIteration: ||returnv||else: ||raiseException('Too many values')|||printsingle(name forname in('bob','fred')

Re: [Python-ideas] Add single() to itertools

2017-10-31 Thread Soni L.
On 2017-10-31 10:58 AM, Joao S. O. Bueno wrote: On 31 October 2017 at 10:52, Steven D'Aprano wrote: On Tue, Oct 31, 2017 at 10:42:23AM -0200, Joao S. O. Bueno wrote: When I need something like this, I usually rop a line on the module namespace that goes like: first = lambda x: next(iter(x))

Re: [Python-ideas] Add single() to itertools

2017-10-31 Thread Joao S. O. Bueno
On 31 October 2017 at 10:52, Steven D'Aprano wrote: > On Tue, Oct 31, 2017 at 10:42:23AM -0200, Joao S. O. Bueno wrote: >> When I need something like this, I usually rop a line on the module >> namespace that goes like: >> >> first = lambda x: next(iter(x)) > > That doesn't meet the requirement th

Re: [Python-ideas] Add single() to itertools

2017-10-31 Thread Steven D'Aprano
On Tue, Oct 31, 2017 at 10:42:23AM -0200, Joao S. O. Bueno wrote: > When I need something like this, I usually rop a line on the module > namespace that goes like: > > first = lambda x: next(iter(x)) That doesn't meet the requirement that x has ONLY one item. And using lambda like that is bad st

Re: [Python-ideas] Add single() to itertools

2017-10-31 Thread Joao S. O. Bueno
When I need something like this, I usually rop a line on the module namespace that goes like: first = lambda x: next(iter(x)) On 30 October 2017 at 23:09, Steven D'Aprano wrote: > On Tue, Oct 31, 2017 at 07:51:02AM +1100, Cameron Simpson wrote: > >> return the(nodes) >> >> It's this kind of th

Re: [Python-ideas] Add single() to itertools

2017-10-31 Thread Greg Ewing
(Reposting this to list -- pushed wrong reply button!) Koos Zevenhoven wrote: |defsingle(i): try: ||v =i.next() |||exceptStopIteration:raiseException('No values')|||try: ||i.next() ||exceptStopIteration: ||returnv||else: ||raiseException('Too many values')|||printsingle(name f

Re: [Python-ideas] Add single() to itertools

2017-10-31 Thread Koos Zevenhoven
> > > |defsingle(i): try: ||v =i.next() > |||exceptStopIteration:raiseException('No > values')|||try: ||i.next() ||exceptStopIteration: ||returnv||else: > ||raiseException('Too many values')| > ||printsingle(name forname in('bob','fred')ifname=='bob')||| | > > ​Now that looks seriously weird.

Re: [Python-ideas] Add single() to itertools

2017-10-30 Thread Chris Angelico
On Tue, Oct 31, 2017 at 3:50 PM, Ivan Pozdeev via Python-ideas wrote: > On 30.10.2017 17:32, Guido van Rossum wrote: >> >> This is a key example of a case where code speaks. Can you write an >> implementation of how you would want single() to work in Python code? >> >> On Mon, Oct 30, 2017 at 2:49

Re: [Python-ideas] Add single() to itertools

2017-10-30 Thread Ivan Pozdeev via Python-ideas
On 30.10.2017 17:32, Guido van Rossum wrote: This is a key example of a case where code speaks. Can you write an implementation of how you would want single() to work in Python code? On Mon, Oct 30, 2017 at 2:49 AM, Ivan Pozdeev via Python-ideas mailto:python-ideas@python.org>> wrote: Th

Re: [Python-ideas] Add single() to itertools

2017-10-30 Thread Steven D'Aprano
On Tue, Oct 31, 2017 at 07:51:02AM +1100, Cameron Simpson wrote: > return the(nodes) > > It's this kind of thing that expresses my intent better than the: > > node, = nodes > return node > > idiom. If the intent is to indicate that there is only one node, then "the(nodes)" fails completely

Re: [Python-ideas] Add single() to itertools

2017-10-30 Thread Cameron Simpson
On 30Oct2017 07:32, Guido van Rossum wrote: This is a key example of a case where code speaks. Can you write an implementation of how you would want single() to work in Python code? Myself, I'm not advocating for putting such a thing in itertools. However, I do have an equivalent utility func

Re: [Python-ideas] Add single() to itertools

2017-10-30 Thread Guido van Rossum
This is a key example of a case where code speaks. Can you write an implementation of how you would want single() to work in Python code? On Mon, Oct 30, 2017 at 2:49 AM, Ivan Pozdeev via Python-ideas < python-ideas@python.org> wrote: > > > On 30.10.2017 9:29, python-ideas-requ...@python.org wrot

Re: [Python-ideas] Add single() to itertools

2017-10-30 Thread Ivan Pozdeev via Python-ideas
On 30.10.2017 9:29, python-ideas-requ...@python.org wrote: If I have understood your use-case, you have a function that returns a list of results (or possibly an iterator, or a tuple, or some other sequence): print(search(haystack, needle)) # prints ['bronze needle', 'gold needle', '

Re: [Python-ideas] Add single() to itertools

2017-10-30 Thread Stéfane Fermigier
IIUC, this would be similar to "first" ( https://pypi.python.org/pypi/first/ ) but would raise exception in case the iterable returns more than one (or less than one) element. Would also be similar to one() in SQLAlchemy queries ( http://docs.sqlalchemy.org/en/latest/orm/query.html#sqlalchemy.orm.

Re: [Python-ideas] Add single() to itertools

2017-10-29 Thread Steven D'Aprano
On Mon, Oct 30, 2017 at 07:14:10AM +0300, Ivan Pozdeev via Python-ideas wrote: > The eponymous C#'s LINQ method, I found very useful in the following, > quite recurring use-case: If I have understood your use-case, you have a function that returns a list of results (or possibly an iterator, or

[Python-ideas] Add single() to itertools

2017-10-29 Thread Ivan Pozdeev via Python-ideas
The eponymous C#'s LINQ method, I found very useful in the following, quite recurring use-case: I need to get a specific element from a data structure that only supports search semantics (i.e. returns a sequence/iterator of results). For that, I specify very precise search criteria, so only tha