Open utterances-bot opened 2 years ago
Seems like a lot of work to exactly simulate
for i in range(0,10,1): print(f”{i= }”)
@chasrmartin there's some things that range-based for loops simply can't do, like:
for (i = 0; i < 100; i += 1):
if data[i].startswith('ignore:'):
i += 1
process(data[i])
But regardless, this was just a curiosity of mine.
for item in data: if item.startswith("ignore:"); continue process(item)
@chasrmartin that is slightly different -- my example only skips the first occurrence of 'ignore:' in a contiguous set of those.
@tusharsadhwani Yes, the code should look like. (btw your code will crash if you need to skip an item on last iteration - typical c-like error with wrong indexing, while safe iterator will just terminate looping)
data = """1
2
ignore: should skip
ignore: shouldn't skip
3
ignore: should skip
ignore: shouldn't crash:)""".splitlines()
def process(item):
print(f"processing {item}")
it = iter(data)
for item in it:
if item.startswith("ignore:"):
item = next(it)
process(item)
Btw I like this tricky experiment, but for Guido sake don't use such a code in production)
@oriontvv
data = """1
2
ignore: should skip
ignore: shouldn't skip
3
ignore: should skip""".splitlines()
def process(item):
print(f"processing {item}")
it = iter(data)
for item in it:
if item.startswith("ignore:"):
item = next(it)
process(item)
Output:
processing 1
processing 2
processing ignore: shouldn't skip
processing 3
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
start(fakepyfile,mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), __main__.__dict__)
File "<string>", line 14, in <module>
StopIteration
Being iterator based doesn't automatically fix the logic (;
I see your point, yeah.
And of course. I sure hope nobody uses this package outside of simple scripts!
Ohh, thanks - I didn't know that previous behavior has been changed with PEP 479 (since python3.5)
Thank you for these insights. I had much fun to follow your exploring path. Keep hacking and don‘t do that at home. 😁
Great Blog! The way you made it possible is amazing.
This is great now I can use this to iterate a list's elements as key inside dictionary and get desirable output
This is great, I had been wondering about how to get the body of a context manager as well as codec use - fun stuff.
@stuaxo interesting! for what usecase did you need a "context-aware" context manager?
Good question, it was a while ago - I can't remember right now, but I feel like it was either the same sort of "use case" as your article, or it might have been something to do with when I was playing with building live coding stuff in python.
Probably more just for the wrongness though.
@tusharsadhwani
for item in it:
if item.startswith("ignore:"):
#item = next(it)
continue
process(item)
This works... ;)
@tinkercnc the behaviour is slightly different if there are two or more ignore lines adjacent to each other
Cool post, and I really like your idea of using a context manager - something I would not have thought about.
Rather than using a codec, you could implement this with an import hook, as described in https://aroberge.github.io/ideas/docs/html/ ;-)
@aroberge indeed! that's one way to solve it. but since this is a language feature I'm trying to implement that has to run in a single python script, in the REPL etc. i wouldn't find that solution very satisfying.
How I added C-style for-loops to Python
Or alternatively: How I made the most cursed Python package of all time.
https://sadh.life/post/cursed-for/