[Python-Dev] Function suggestion: itertools.one()

2020-07-27 Thread Noam Yorav-Raphael
Hi,

There's a simple function that I use many times, and I think may be a good
fit to be added to itertools. A function that gets an iterator, and if it
has exactly one element returns it, and otherwise raises an exception. This
is very useful for cases where I do some sort of query that I expect to get
exactly one result, and I want an exception to be raised if I'm wrong. For
example:

jack = one(p for p in people if p.id == '1234')

sqlalchemy already has such a function for queries:
https://docs.sqlalchemy.org/en/13/orm/query.html#sqlalchemy.orm.query.Query.one

This is my implementation:

def one(iterable):
it = iter(iterable)
try:
r = next(it)
except StopIteration:
raise ValueError("Iterator is empty")
try:
next(it)
except StopIteration:
return r
else:
raise ValueError("Iterator has more than one item")

What do you think?

Thanks,
Noam
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/D52MPKLIN4VEXBOCKVMTWAK66MAOEINY/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: Function suggestion: itertools.one()

2020-07-27 Thread Noam Yorav-Raphael
Thanks for the suggestion! I agree that using a list is clearer that having
a trailing comma, I like it!

I still think that having a one() function would be useful, since:
1. I think it spells the intention more clearly. Also the exception would
be easier to understand, since errors in tuple unpacking usually mean
something else.
2. The one() function allows you to use the result inside an expression
without assigning it to a variable.

Cheers,
Noam

On Mon, Jul 27, 2020 at 10:30 PM Chris Angelico  wrote:

> On Tue, Jul 28, 2020 at 5:24 AM Steven Barker  wrote:
> >
> > A single-name unpacking assignment can do exactly what you want, albeit
> with slightly less helpful exception messages:
> >
> > jack, = (p for p in people if p.id == '1234') # note comma after
> the name jack
> >
>
> Agreed. As a minor readability refinement, I would prefer to spell
> this with square brackets:
>
> [jack] = (p for p in people if p.id == '1234')
>
> The effect is identical, but for the one-item unpack, I prefer not to
> have the vital-but-subtle trailing comma.
>
> (You can use square brackets on larger assignment lists too, but they
> don't add anything.)
>
> ChrisA
> ___
> Python-Dev mailing list -- python-dev@python.org
> To unsubscribe send an email to python-dev-le...@python.org
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-dev@python.org/message/62ZAKSHT6VHT66CHFBPJHHAUQSW2S3DK/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/LHJZWOE7S4TQDZ5FTEN3GKK2Y3J6WFH3/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: Function suggestion: itertools.one()

2020-07-28 Thread Noam Yorav-Raphael
I'm sorry, I don't think I got that. I don't find a mailing list called
itertools-ideas. Do you mean this should go to python-ideas? If so, I'm
sorry for bothering python-dev, and I'll move this to python-ideas.

I just read "The python-ideas list is for discussing more speculative
design ideas" (https://www.python.org/community/lists/) as meaning
discussing ideas for significant design changes, and not for adding a small
function.

Thanks,
Noam

>
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/67MPZN4ZB7JXR47HGJWW66TIJCOQ4AEZ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: Function suggestion: itertools.one()

2020-07-28 Thread Noam Yorav-Raphael
Thanks! I opened a new thread in python-ideas, here:
https://mail.python.org/archives/list/python-id...@python.org/thread/6OLEL4XTUWXRI7ENODKEDOYFBRVDYKI7/

The "first" thread was very long, and was focused on a
different function, first(). Perhaps a new thread, focused on one simple
function in itertools, can reach a conclusion more easily.

Thanks,
Noam
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/NK2ERT4Z3T2LLSMEHTAOMYJBLIDY7FTV/
Code of Conduct: http://python.org/psf/codeofconduct/