Python is a great language. High level, easy to learn, and easy to code in. Although I don't find it very useful for GUI apps, it's increadibly easy to program purposeful console apps with it. Sometimes, however, it's fun to throw in some cool effects into your script. Here's some to try out :)
import sys, time for character in some_text: sys.stdout.write(character) sys.stdout.flush() time.sleep(.03)
This code will take some_text and print it out slowly, all on one line (unless of course some_text contains a \n). slow_text must be a string. If you use this code along with "os.system('clear')" (which clears the terminal in linux), you can make your program look really sci-fi.
import sys, time sys.stdout.write("Loading.... ") sys.stdout.flush() round = 0 type = 0 while round != 15: if type == 0: sys.stdout.write("\b/") if type == 1: sys.stdout.write("\b-") if type == 2: sys.stdout.write("\b\\") if type == 3: sys.stdout.write("\b|") type += 1 round += 1 if type == 4: type = 0 sys.stdout.flush() time.sleep(0.2) print "\b\b done!"
This code will print out "Loading.... " and then at the end of the line will show a little spinning line (made out of /,|,\, and -) that will spin for 15 rounds before it gets replaced with the word "done!". Unfortunately, all this does at the moment is look cool. If you wanted to have it actually represent the loading progress of some background code, you would need to involve threading, which can become quite an advanced topic on it's own. Luckily for you, I've already done the work for you! You can find a threaded example of this code just below here."
import threading, sys, time class loading_bar(threading.Thread): def run(self): global stop, kill sys.stdout.write("Loading.... ") sys.stdout.flush() type = 0 while stop != True: if type == 0: sys.stdout.write("\b/") if type == 1: sys.stdout.write("\b-") if type == 2: sys.stdout.write("\b\\") if type == 3: sys.stdout.write("\b|") type += 1 if type == 4: type = 0 sys.stdout.flush() time.sleep(0.2) if kill == True: print "\b\b\b\b Aborting!" else: print "\b\b done!" stop = kill = False loading_bar_control = loading_bar() loading_bar_control.start() try: ################################## # Your processing code goes here # ################################## except KeyboardInterrupt or EOFError: kill = True; stop = True stop = True
As you can see the code here is somewhat more in depth. We subclass the Thread class and overwrite the run method with the Spinning loading bar code, however instead of giving it a timeout, we wait until the variable global variable stop is turned True. So in a test run it would run the loading bar, run your code, and then issue the 'stop = True' assignment and the loading bar will stop. You may have noticed your code has been put into a try statement, this is because if somebody pressed ctrl+C, it would stop your code, and then the loading bar would still produce "done!". However, I added in a extra variable called kill which the loading_bar class will check to see if it was killed or not on exit. If kill is True, it goes backspace 4 times (instead of 2), and prints "Aborting!". It backspaces 4 times instead of 2 because some terminals print out the ^C, and we don't want that to show. You might want to add in some code after the kill = True, because if the user cancels than he probably wants to get out of the program, rather than continueing.
import sys, time, os text = """This is a test! We will see how it goes, because I'm HOPING that it will write ALL this text sdrawkcab ;) """ def print_backwards(text,speed = 0.05): lines = text.split("\n") lines.reverse() for line in lines: sys.stdout.write("\n") for line in lines: sys.stdout.write("\x1b[A") # Move the cursor up one line = list(line) line.reverse() length = len(line) output = [] for null in range(length): output.append(" ") count = 0 for replacement_character in line: output[(length-count)-1] = replacement_character for character in output: sys.stdout.write(character) sys.stdout.flush() time.sleep(speed) for null in range(length): sys.stdout.write("\b") count+=1 for line in range(len(lines)-1): print # Pushes the cursor back down to the end return print_backwards(text)
This little bit of code took me surprizingly longer to do than I figured it would have. The function print_backwards takes the text you enter, moves the cursor down the appropriate ammount of lines, then (for each line) it creates a list called output. Output is filled with spaces the length of the line, and as it goes along it replaces the farmost space with the appropriate character, prints all of output, delays for a specified ammount of time, and then repeats until the line has been completely written. This generates the effect of having the text written backwards. When finished on the line, it moves the cursor upwards and repeats until there is no more lines left. Finally, it moves the cursor all the way back down to the end again, to make room for the prompt or whatever comes next.
There we go! Hopefully I've supplied you with some fun new things to try out in your python console apps. Each section of code has it's own specific drawbacks and advantages, so you'll probably have to play around with the code to get the "exact" effect you'd like. However, the code here should offer some great starting points and ideas for future projects. Happy coding!
Stealth-
Posted by Cypher, on 12/02/2009
Fix the indentation and it will be easier to read. Nice otherwise though. I didn't know printing text backwards was possible in python!
Posted by Stealth- (Admin), on 12/31/1969
Thank's for pointing that out. The indentation has been fixed.
Posted by Ty, on 08/03/2011
Thank you!
Posted by left0, on 08/11/2011
i seem to remember a few years ago looking at a python for kids tutorial that included designing simple games and the coding graphics through python
Posted by cydrindpync, on 09/18/2011
big thanks to the author