[Python-ideas] Re: s?scanf [was: A string function idea]

2022-03-29 Thread Chris Angelico
On Wed, 30 Mar 2022 at 15:11, Stephen J. Turnbull wrote: > > Chris Angelico writes: > > > [fruit] = sscanf(sample, "%*sfruit:%s\n") > > I'm warming to this idea. It does hit the sweet spot of doing exactly > what you want -- except when it can't do what you want at all. :-) > It's concise and

[Python-ideas] Re: A string function idea

2022-03-29 Thread Steven D'Aprano
On Tue, Mar 29, 2022 at 12:35:56AM -0700, Paul Bryan wrote: > I wonder if applying regular expressions would sufficiently address > your use case. 'Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.' -- Jamie Zawinski Apart

[Python-ideas] Re: A string function idea

2022-03-29 Thread Christopher Barker
On Tue, Mar 29, 2022 at 4:08 PM Steven D'Aprano wrote: > I have no strong opinion on whether this simple function should be built > into the string class, I do -- this is not sufficiently general to be a string method. > but I do have a strong opinion about re-writing > it into a slower,

[Python-ideas] Re: A string function idea

2022-03-29 Thread Steven D'Aprano
On Tue, Mar 29, 2022 at 11:00:41AM +0300, Serhiy Storchaka wrote: > 28.03.22 15:13, StrikerOmega пише: > >And I want to grab some kind of value from it. > > There is a powerful tool designed for solving such problems. Is is > called regular expressions. > > >sample.grab(start="fruit:",

[Python-ideas] Re: Anonymous namedtuples, revisited

2022-03-29 Thread Ethan Furman
On 3/29/22 14:41, Christopher Barker wrote: > Ethan Furman queried: >> The `__dict__` is needed to store the field names -- did you add `__dict__` to the >> `__slots__`? > > Nope — though the error indicated that I couldn’t add anything to __slots__ when subclassing tuple. But I may have >

[Python-ideas] Re: A string function idea

2022-03-29 Thread Chris Angelico
On Wed, 30 Mar 2022 at 10:08, Steven D'Aprano wrote: > Here's the version of grab I used: > > def grab(text, start, end): > a = text.index(start) > b = text.index(end, a+len(start)) > return text[a+len(start):b] > This is where Python would benefit from an sscanf-style parser.

[Python-ideas] Re: Anonymous namedtuples, revisited

2022-03-29 Thread Christopher Barker
> >> The `__dict__` is needed to store the field names -- did you add > `__dict__` to the > >> `__slots__`? Nope — though the error indicated that I couldn’t add anything to __slots__ when subclassing tuple. But I may have misunderstood the error. Also, frankly, I’m completely confused by

[Python-ideas] Re: A string function idea

2022-03-29 Thread Steven D'Aprano
On Tue, Mar 29, 2022 at 09:12:36AM +0200, StrikerOmega wrote: > The grab function would find the index of the first occurrence of the > "start" string in the parent string and then the next occurrence of the > "end" string starting from that index and return the substring between > those. That's

[Python-ideas] s?scanf [was: A string function idea]

2022-03-29 Thread Stephen J. Turnbull
Chris Angelico writes: > [fruit] = sscanf(sample, "%*sfruit:%s\n") I'm warming to this idea. It does hit the sweet spot of doing exactly what you want -- except when it can't do what you want at all. :-) It's concise and quite powerful, applicable to many common use cases. I do have one

[Python-ideas] Re: Anonymous namedtuples, revisited

2022-03-29 Thread Joseph Martinot-Lagarde
@Michael: As the previous author I fully support this proposal! ;) Thank you for the revival. For reference, since you linked one of the answer of the original thread, this is my original proposal: https://mail.python.org/pipermail/python-ideas/2016-April/039836.html Both suggested

[Python-ideas] Re: mro and super don't feel so pythonic

2022-03-29 Thread Steven D'Aprano
On Tue, Mar 29, 2022 at 06:23:06AM +1100, Chris Angelico wrote: > If I'm reading this correctly, you're defining a "conflict" as finding > two implementations of the same-named method in independent subtrees > of inheritance - that is, when you say "class Pizza(Crust, Topping):", > you're

[Python-ideas] Re: mro and super don't feel so pythonic

2022-03-29 Thread Chris Angelico
On Tue, 29 Mar 2022 at 21:34, Steven D'Aprano wrote: > > On Tue, Mar 29, 2022 at 06:23:06AM +1100, Chris Angelico wrote: > > > If I'm reading this correctly, you're defining a "conflict" as finding > > two implementations of the same-named method in independent subtrees > > of inheritance - that

[Python-ideas] Re: Anonymous namedtuples, revisited

2022-03-29 Thread Ricky Teachey
I think I'd often reach for an optimized version of something like this over SimpleNamespace or even a Dataclass if it existed. On Tue, Mar 29, 2022, 2:04 AM Christopher Barker wrote: > Finally got around to fleshing out my idea here. > > My thought was to make an anonymous names tuple not as a

[Python-ideas] Re: Anonymous namedtuples, revisited

2022-03-29 Thread Joao S. O. Bueno
On Fri, Mar 25, 2022 at 12:17 AM Brendan Barnwell wrote: > On 2022-03-24 10:43, Andrew Svetlov wrote: > > The proposal doesn't work well with type hints: atuple(a=1, b=2) and > > atuple(a="a", b="b") generates the same type. > > I'm neither here nor there on the original proposal, but I

[Python-ideas] Re: mro and super don't feel so pythonic

2022-03-29 Thread Stephen J. Turnbull
malmiteria writes: > class An: > def method(self): > print('An') > class A[n-1](An): pass > ... > class A1(A2): pass > class A0(A1): > def method(self): > self.__as_parent__(An).method() > > A0().method() prints 'An' Of course it prints 'An', and so does class A00(A1):

[Python-ideas] Re: A string function idea

2022-03-29 Thread Abdulla Al Kathiri
Thanks for teaching me about the __getitem__ method of the Match class. I always do .group(1) before. Abdulla Sent from my iPhone > On 29 Mar 2022, at 12:02 PM, Serhiy Storchaka wrote: > > re.search(r'fruit:(.*?)\n', sample)[1] ___ Python-ideas

[Python-ideas] Re: Anonymous namedtuples, revisited

2022-03-29 Thread Christopher Barker
Finally got around to fleshing out my idea here. My thought was to make an anonymous names tuple not as a new anonymous class, but simply as an instance with specific field names. So all of these would the the same class, but they would also be lightweight, and all subclasses of tuple -- from a

[Python-ideas] Re: Anonymous namedtuples, revisited

2022-03-29 Thread Christopher Barker
On Tue, Mar 29, 2022 at 6:37 AM Joseph Martinot-Lagarde < contreba...@gmail.com> wrote: > Both suggested implementations are very interesting. If I'm not wrong they > have exactly the same behavior (except for __dict__). > pretty close, yes -- probably differences with pickling, and if you did

[Python-ideas] Re: Anonymous namedtuples, revisited

2022-03-29 Thread Ethan Furman
On 3/29/22 09:14, Christopher Barker wrote: > [...] I tried to use __slots__ in TupleWithNames, but apparently you can't use __slots__ in a > tuple subclass ('cause tuple's already using it ??) -- but that could be done in a builtin. > then it wouldn't need a __dict__ The `__dict__` is needed

[Python-ideas] Re: Anonymous namedtuples, revisited

2022-03-29 Thread Ethan Furman
On 3/29/22 09:57, Ricky Teachey wrote: > On Tue, Mar 29, 2022 at 12:49 PM Ethan Furman wrote: >> The `__dict__` is needed to store the field names -- did you add `__dict__` to the >> `__slots__`? (Of course, if you've added `__dict__` then you lose the limited size >> of `__slots__`.) > >

[Python-ideas] Re: Anonymous namedtuples, revisited

2022-03-29 Thread Ricky Teachey
On Tue, Mar 29, 2022 at 12:49 PM Ethan Furman wrote: > On 3/29/22 09:14, Christopher Barker wrote: > > > [...] I tried to use __slots__ in TupleWithNames, but apparently you > can't use __slots__ in a > > tuple subclass ('cause tuple's already using it ??) -- but that could > be done in a

[Python-ideas] Re: mro and super don't feel so pythonic

2022-03-29 Thread malmiteria
Alright everyone. First of all, thanks for all your answers, I really appreciate the discussion we're having. Turns out i won't have so much free time tonight, and probably tomorrow night either, so i won't be able to answer the last few comments before some then. I apologise if i'm missing

[Python-ideas] Re: A string function idea

2022-03-29 Thread Serhiy Storchaka
28.03.22 15:13, StrikerOmega пише: And I want to grab some kind of value from it. There is a powerful tool designed for solving such problems. Is is called regular expressions. sample.grab(start="fruit:", end="\n") >> 'apple' re.search(r'fruit:(.*?)\n', sample)[1]

[Python-ideas] Re: mro and super don't feel so pythonic

2022-03-29 Thread Robert Collins
On Sat, 26 Mar 2022 at 18:01, malmiteria wrote: > Hi, > > Before anything, i made a github repository about this topic here : > https://github.com/malmiteria/super-alternative-to-super > > The core of what i wanna discuss here is that i don't think mro and super > (mainly because it relies on

[Python-ideas] Re: Allow `return yield from`

2022-03-29 Thread Patrick Reader
On 29/03/2022 03:18, Michael Smith wrote: On Mon, Mar 28, 2022 at 16:06 Patrick Reader wrote: I would like to be able to use a `yield from` expression in a `return` statement without parentheses, as a small quality of life tweak, i.e.: return yield from gen instead of

[Python-ideas] Re: A string function idea

2022-03-29 Thread Chris Angelico
On Tue, 29 Mar 2022 at 18:13, StrikerOmega wrote: > > The grab function would find the index of the first occurrence of the "start" > string in the parent string and then the next occurrence of the "end" string > starting from that index and return the substring between those. > This sounds

[Python-ideas] Re: A string function idea

2022-03-29 Thread StrikerOmega
The grab function would find the index of the first occurrence of the "start" string in the parent string and then the next occurrence of the "end" string starting from that index and return the substring between those. So in the example: sample = "sqrt(sin(x) + cos(y))" The grab function would

[Python-ideas] Re: A string function idea

2022-03-29 Thread Paul Bryan
I wonder if applying regular expressions would sufficiently address your use case. On Mon, 2022-03-28 at 14:13 +0200, StrikerOmega wrote: > Hello everyone, > > When I am coding in Python I often encounter situations where I have > a string like this one. > > sample=""" > fruit:apple >