[Python-ideas] Re: Fwd: Re: Fwd: re.findfirst()

2019-12-07 Thread Juancarlo Añez
> > > > Well, match objects already accept subscripting as an alternative to > .group(), so: > > re.search(...)[0] > The semantics are not the same as those of: re.findall(...)[0] Subscripting a Match object will yield the match for a single group, which is always a string, while the fir

[Python-ideas] Re: Fwd: Re: Fwd: re.findfirst()

2019-12-07 Thread Kyle Stanley
Serhiy Storchaka wrote: > My concern is that this will add complexity to the module documentation > which is already too complex. re.findfirst() has more complex semantic > (if no capture groups returns this, if one capture group return that, > and in other cases return even something of different

[Python-ideas] Re: Fwd: Re: Fwd: re.findfirst()

2019-12-07 Thread Kyle Stanley
> Code examples should of course be used sparingly, but I think re.finditer() could benefit from at least one Clarification: I see that there's an example of it being used in https://docs.python.org/3.8/library/re.html#finding-all-adverbs-and-their-positions and one more complex example with https

[Python-ideas] Re: Argumenting in favor of first()

2019-12-07 Thread Kirill Balunov
сб, 7 дек. 2019 г. в 03:45, Steven D'Aprano : > This is how I would implement the function in Python: > > def first(iterable, default=None): > return next(iter(iterable), default) > A somewhat related discussion was somewhere inside November 2017

[Python-ideas] Re: Argumenting in favor of first()

2019-12-07 Thread Wes Turner
+1 for itertools.first(seq, default=Exception) *and* itertools.one(seq, default=Exception) This is a very common pattern (particularly with RDF / JSON-LD (@type) where there can be multiple instances of any predicate-object / attribute-value pair) SQLAlchemy also has .first(), .one(), and .one_or

[Python-ideas] Re: Fwd: Re: Fwd: re.findfirst()

2019-12-07 Thread Random832
On Sat, Dec 7, 2019, at 01:43, Serhiy Storchaka wrote: > This is incompatible with subscripting. match[0] returns match.group(0), > not match.groups()[0]. And dict[0] returns the value whose key is 0, not the first key of the dictionary. set[0] does not work at all. there is no general guarante

[Python-ideas] Re: Fwd: Re: Fwd: re.findfirst()

2019-12-07 Thread Kyle Stanley
> Alternatively: creating a new section under https://docs.python.org/3.8/library/re.html#regular-expression-examples, titled "Finding the first match", where it briefly explains the difference in behavior between using re.findall()[0] and re.finditer().group(1) (or re.finditer.group() when there's

[Python-ideas] Re: Fwd: Re: Fwd: re.findfirst()

2019-12-07 Thread Andrew Barnert via Python-ideas
> On Dec 7, 2019, at 04:51, Kyle Stanley wrote: > Alternatively: creating a new section under > https://docs.python.org/3.8/library/re.html#regular-expression-examples, > titled "Finding the first match", where it briefly explains the difference in > behavior between using re.findall()[0] and r

[Python-ideas] Re: Argumenting in favor of first()

2019-12-07 Thread Andrew Barnert via Python-ideas
On Dec 7, 2019, at 07:33, Wes Turner wrote: > >  > +1 for itertools.first(seq, default=Exception) *and* itertools.one(seq, > default=Exception) What does default=Exception mean? What happens if you pass a different value? Does it do one thing if the argument is a type that’s a subclass of Exc

[Python-ideas] Re: Argumenting in favor of first()

2019-12-07 Thread Wes Turner
On Sat, Dec 7, 2019, 8:20 PM Andrew Barnert wrote: > On Dec 7, 2019, at 07:33, Wes Turner wrote: > > > >  > > +1 for itertools.first(seq, default=Exception) *and* itertools.one(seq, > default=Exception) > > What does default=Exception mean? What happens if you pass a different > value? Does it

[Python-ideas] Re: Fwd: Re: Fwd: re.findfirst()

2019-12-07 Thread Kyle Stanley
> Hold on, what is finditer().group(1) supposed to mean here? You’d need next(finditer()).group(1) or next(m.group(1) for m in finditer()) or something. That was a mistake, I intended to write re.search().group(1), not re.finditer().group(1). I clarified this in another reply to the thread about a

[Python-ideas] Re: Argumenting in favor of first()

2019-12-07 Thread Andrew Barnert via Python-ideas
On Dec 7, 2019, at 18:09, Wes Turner wrote: > > >> On Sat, Dec 7, 2019, 8:20 PM Andrew Barnert wrote: >> On Dec 7, 2019, at 07:33, Wes Turner wrote: >> > >> >  >> > +1 for itertools.first(seq, default=Exception) *and* itertools.one(seq, >> > default=Exception) >> >> What does default=Excep