def mapFirst(stream, transform):
try:
first = next(stream)
except StopIteration:
return
yield transform(first)
yield from stream
def mapLast(stream, transform):
try:
previous = next(stream)
except StopIteration:
return
for item in stream:
yield previous
previous = item
yield transform(previous)
def main():
for each in (iterEmpty, iter1, iter2, iterMany):
baseIterator = each()
chopFirst = mapFirst(baseIterator, lambda x: x[1:-1])
andCapLast = mapLast(chopFirst, lambda x: x.upper())
print(repr(" ".join(andCapLast)))
Is this idiomatic? Especially my implementations of mapFirst and mapList there in the middle? Or is there some way to pull this off that is more elegant?
I've been doing more with iterators and stacking them (probably because I've been playing with Elixir elsewhere), I am generally curious what the performance tradeoffs of heavy use of iterators and yield functions in python is. I know the argument foravoiding big list copies when moving between stages. Is it one of those things where there's also some overhead with them, where for small stuff, you'd just be better list-ifying the first iterator and then working with lists (where, for example, I could
Chris Angelico schreef op 14/06/2022 om 20:47:
def main():
for each in (iterEmpty, iter1, iter2, iterMany):
baseIterator = each()
chopFirst = mapFirst(baseIterator, lambda x: x[1:-1])
andCapLast = mapLast(chopFirst, lambda x: x.upper())
print(repr(" ".join(andCapLast)))
Don't bother with a main() function unless you actually need to beNot (necessarily) a main function, but these days the general
able to use it as a function. Most of the time, it's simplest to just
have the code you want, right there in the file. :) Python isn't C or
Java, and code doesn't have to get wrapped up in functions in order to exist.
recommendation seems to be to use the "if __name__ == '__main__':"
construct, so that the file can be used as a module as well as as a
script. Even for short simple things that can be helpful when doing
things like running tests or extracting docstrings.
Not (necessarily) a main function, but these days the generaldef main():
for each in (iterEmpty, iter1, iter2, iterMany):
baseIterator = each()
chopFirst = mapFirst(baseIterator, lambda x: x[1:-1])
andCapLast = mapLast(chopFirst, lambda x: x.upper())
print(repr(" ".join(andCapLast)))
Don't bother with a main() function unless you actually need to be
able to use it as a function. Most of the time, it's simplest to just
have the code you want, right there in the file. :) Python isn't C or
Java, and code doesn't have to get wrapped up in functions in order to
exist.
If it does need to be used as a module as well as a script, sure. But
(a) not everything does, and (b) even then, you don't need a main()
On Wed, 15 Jun 2022 at 05:45, Roel Schroeven <roel@roelschroeven.net> wrote: >> Not (necessarily) a main function, but these days the general
recommendation seems to be to use the "if __name__ == '__main__':"
construct, so that the file can be used as a module as well as as a
script. Even for short simple things that can be helpful when doing
things like running tests or extracting docstrings.
If it does need to be used as a module as well as a script, sure. But
(a) not everything does, and (b) even then, you don't need a main()
function; what you need is the name-is-main check. The main function
is only necessary when you need to be able to invoke your main entry
point externally, AND this main entry point doesn't have a better
name. That's fairly rare in my experience.
Don't bother with a main() function unless you actually need to be
able to use it as a function. Most of the time, it's simplest to
just have the code you want, right there in the file. :) Python
isn't C or Java, and code doesn't have to get wrapped up in
functions in order to exist.
Actually a main() function in Python is pretty useful, because Python
code on the top level executes a lot slower. I believe this is due to
global variable lookups instead of local.
On Wed, 15 Jun 2022 04:47:31 +1000, Chris Angelico wrote:
Don't bother with a main() function unless you actually need to be
able to use it as a function. Most of the time, it's simplest to
just have the code you want, right there in the file. :) Python
isn't C or Java, and code doesn't have to get wrapped up in
functions in order to exist.
Actually a main() function in Python is pretty useful, because Python
code on the top level executes a lot slower. I believe this is due to
global variable lookups instead of local.
Here is benchmark output from a small test.
```
Benchmark 1: python3 test1.py
Time (mean ± σ): 662.0 ms ± 44.7 ms
Range (min … max): 569.4 ms … 754.1 ms
Benchmark 2: python3 test2.py
Time (mean ± σ): 432.1 ms ± 14.4 ms
Range (min … max): 411.4 ms … 455.1 ms
Summary
'python3 test2.py' ran
1.53 ± 0.12 times faster than 'python3 test1.py'
```
Contents of test1.py:
```
l1 = list(range(5_000_000))
l2 = []
while l1:
l2.append(l1.pop())
print(len(l1), len(l2))
```
Contents of test2.py:
```
def main():
l1 = list(range(5_000_000))
l2 = []
while l1:
l2.append(l1.pop())
print(len(l1), len(l2))
main()
```
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 285 |
Nodes: | 16 (2 / 14) |
Uptime: | 70:00:13 |
Calls: | 6,488 |
Calls today: | 1 |
Files: | 12,096 |
Messages: | 5,275,483 |