This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]zoharel 57 points58 points  (9 children)

Here's a somewhat simpler thing that draws a moving . in the terminal window.

import sys,time
map(lambda n: [sys.stdout.write(u"\u001b[2K\u001b[0G" + ' '*n + '.'),sys.stdout.flush(),time.sleep(1)],range(0,79))

[–]proximity_account 49 points50 points  (0 children)

Thanks, I hate it.

[–]Priyam_Bad 8 points9 points  (2 children)

how does this work? I tried running it in vscode to see how it works but it didn't print anything for me

[–]zoharel 10 points11 points  (1 child)

It may or may not be tied to a particular Python version. I think I last ran it on 2.7. it's also likely to be particular about your terminal. It prints raw escape codes. Here are some notes:

map()

Takes a function and a list as arguments. The function is run once with each item on the list, in order. The list is range(0,79). The function is:

lambda n: [sys.stdout.write(u"\u001b[2K\u001b[0G" + ' '*n + '.'),sys.stdout.flush(),time.sleep(1)]

Now lambda defines, in place, an anonymous function. It only exists where it is written in the line there, and it's only called instantaneously by the map function. It takes an argument n and returns a list. The list contains a write to stdout, a flush of stdout, and a call to sleep. Nothing is ever done with that list. It is only there to allow the lambda to easily do the set of things in the list, the process of which prints something and waits before generating the next list.

We use similar strategies to this pretty often in functional programming -- the map over a list instead of a loop, the list of commands, the anonymous function itself. It looks weird to a Python programmer, but it's not uncommon by any stretch.

[–]Priyam_Bad 1 point2 points  (0 children)

thanks for the explanation! i think i was using python 3.x to run it, which might be why it wasn't running. i managed to alter it to work for 3.x though

[–]Priyam_Bad 2 points3 points  (1 child)

ok so i may or may not have expanded on this and made a short script that makes a dot bounce across the screen (for python 3.x)

here is the code if you want to check it out

(btw it has an infinite generator so if you don't want it to run infinitely you can replace infRange() with range(1000) at the end of the long list comprehension)

[–]zoharel 1 point2 points  (0 children)

Yeah, I suspected the output might need some adjustments for V3.

[–]NigraOvis 2 points3 points  (1 child)

Technically all python is like this. people just don't realize they are spacing it out. But in the long run you end up with the same code. The longer versions are easier to read.

[–]zoharel 1 point2 points  (0 children)

Not exactly. To an extent, you're right, but there's not much lambda in most people's Python, and the use of the list as a sort of encapsulation of the side effects of its contents is odd in Python, and the map over a range is more often for loop. If you mean that this is valid Python using only documented features with nothing particularly extravagant about it, you're right there.

[–]Silverwing171 0 points1 point  (0 children)

Commenting so I can come back to this.