[Python-Dev] Re: Feature Suggestion: "repeat" statement in loops

2023-01-28 Thread Matthias Görgens
I don't think 'repeat' should make it into the language.

But: it's an excellent test case to check how flexible the language already
is. Joao did a great job demonstrating what's possible!

On Thu, 26 Jan 2023, 21:15 Joao S. O. Bueno,  wrote:

> I don't think this will fly - if not for any other reason, it is a very
> rare pattern
> to take place alongside such important flow-control statements as
> continue and break
>
> But for your convenience, here is a small wrapper that, along with the
> walrus operator, could be used when you need that functionality:
>
> ```
> class Repeatable:
> def __init__(self, it):
> self.it = it
> self.repeat_last = False
> self.last_item = None
> def repeat(self):
> self.repeat_last = True
> def __iter__(self):
> for item in self.it:
> while self.repeat_last:
> self.repeat_last = False
> yield self.last_item
> self.last_item = item
> yield item
>
>
> test = 1
> for x in (rx:=Repeatable(range(3))):
> print(x)
> if x == test:
> test = -1
> rx.repeat()
> ```
>
> On Thu, Jan 26, 2023 at 4:41 PM Thomas Ratzke 
> wrote:
>
>> Hi all,
>>
>> i would like to suggest the following Python feature. It naturally
>> happens that one want's to repeat the current iteration of a for loop
>> for example after an error happened. For this purpose, I usually set a
>> flag and put a while loop inside my for loop. A simple "repeat"
>> statement just like "continue" or "break" would make the code much more
>> readable.
>>
>>
>> This is my solution at the moment with A being checked:
>>
>> for _ in range(n):
>>  flag = True
>>  while flag:
>>  ...
>>  if A:
>>  flag = False # go to next iteration
>>
>>
>> I would suggest the repeat statement in the following sense
>>
>> for _ in range(n):
>>  ...
>>  if not A:
>>  repeat # repeat current iteration
>>
>> Notice the "not" in the if clause. I am really looking forwars to hear
>> your opinions.
>>
>> Best regards
>> Thomas
>>
>> ___
>> 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/LNER4MH6IT6HBFKFVTUOJ225PTCZSRRC/
>> 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/WWEJQD7IIPNC4FUSPHLXEH7SVN6EVK6H/
> 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/AJLFDS7RACGYZTGW4U4EAGP5DYMSOJDW/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: A proposal to modify `None` so that it hashes to a constant

2022-11-28 Thread Matthias Görgens
For what it's worth, as a user of the language I would like sets to behave
as much as possible as-if they were basically dicts that map all elements
to `()`.  That way I'd have to keep one less mental model in my head.

I deliberately say 'as-if' because when I'm a user of the language, I don't
care how it's implemented.  (Just like I don't have to care as a user that
we have (at least) two different ways dicts are represented internally.)
___
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/WB5VJHTCEPDLVQV46YDXO3TRBDXJCK5S/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: NEWLINE sentinel behavior in CPython's PEG grammar

2022-10-26 Thread Matthias Görgens
Hi David,

Could you share what you have so far, perhaps ok GitHub or so? That way
it's easier to diagnose your problems. I'm reasonably familiar with Rust.

Perhaps also add a minimal crashing example?

Cheers,
Matthias.

On Thu, 27 Oct 2022, 04:52 David J W,  wrote:

> Pablo,
> Nl and Newline are tokens but I am interested in NEWLINE's behavior in
> the Python grammar, note the casing.
>
> For example in simple_stmts @
> https://github.com/python/cpython/blob/main/Grammar/python.gram#L107
>
> Is that NEWLINE some sort of built in rule to the grammar?   In my project
> I am running into problems where the parser crashes any time there is some
> double like NL & N or Newline & NL but I want to nail down NEWLINE's
> behavior in CPython's PEG grammar.
>
> On Wed, Oct 26, 2022 at 12:51 PM Pablo Galindo Salgado <
> pablog...@gmail.com> wrote:
>
>> Hi,
>>
>> I am not sure I understand exactly what you are asking but NEWLINE is a
>> token, not a parser rule. What decides when NEWLINE is emitted is the lexer
>> that has nothing to do with PEG. Normally PEG parsers also acts as
>> tokenizers but the one in cpython does not.
>>
>> Also notice that CPython’s parser uses a version of the tokeniser written
>> in C that doesn’t share code with the exposed version. You will find that
>> the tokenizer module in the standard library actually behaves differently
>> regarding what tokens are emitted in new lines and indentations.
>>
>> The only way to be sure is check the code unfortunately.
>>
>> Hope this helps.
>>
>> Regards from rainy London,
>> Pablo Galindo Salgado
>>
>> > On 26 Oct 2022, at 19:12, David J W  wrote:
>> >
>> > 
>> > I am writing a Rust version of Python for fun and I am at the parser
>> stage of development.
>> >
>> > I copied and modified a PEG grammar ruleset from another open source
>> project and I've already noticed some problems (ex Newline vs NL) with how
>> they transcribed things.
>> >
>> > I am suspecting that CPython's grammar NEWLINE is a builtin rule for
>> the parser that is something like `(Newline+ | NL+ ) {NOP}` but wanted to
>> sanity check if that is right before I figure out how to hack in a NEWLINE
>> rule and update my grammar ruleset.
>> > ___
>> > 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/NMCMEDMEBKATYKRNZLX2NDGFOB5UHQ5A/
>> > 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/LTDXZ4DS2GLICZRWYZ5PVLPBJHVGQPSS/
> 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/ZZDKWS62QG3BTNIT2NYRCLRI4VJ2HBF6/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: How about using modern C++ in development of CPython ?

2022-10-17 Thread Matthias Görgens
CPython is written (mostly) in C.

There's Jython written in Java. There also IronPython written on C#.

Feel free to write your own Python implementation in C++. Could be fun!
___
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/TF2ODTVTNJFMPXJJKSISHFGGRDNXDYMR/
Code of Conduct: http://python.org/psf/codeofconduct/