Last active
August 29, 2015 14:25
-
-
Save magopian/b46bd34c86d185a82b92 to your computer and use it in GitHub Desktop.
Auto-reload turtle code on file save (https://docs.python.org/3/library/turtle.html)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Reloads the `my_turtle.py` code on save. | |
Put simple turtle instructions in the `my_turtle.py` file, | |
and they'll be re-run (on a clean window) on each file save. | |
Usage: | |
1/ put some turtle instructions in a `my_turtle.py` file | |
(eg `turtle.forward(100)`) | |
2/ run `python watch_turtle.py` on a commandline | |
(no dependencies needed) | |
3/ play around by adding/modifiying some turtle instructions | |
in the `my_turtle.py` file | |
4/ when you're happy with your modifications, save the file | |
(it'll empty the turtle screen and re-run the instructions) | |
5/ back to 3/ | |
Stop by pressing CTRL+C in the terminal. | |
""" | |
import os | |
import traceback | |
import sys | |
import time | |
import turtle | |
# Get the last modification time. Not as effective as some | |
# libs out there, but doesn't needs dependencies. | |
get_last_mod = lambda: os.stat('my_turtle.py').st_mtime | |
# Fancier than the original arrow shape. | |
turtle.shape('turtle') | |
# Initialize with a dummy value. | |
last_modification = 0 | |
while True: # Run until a KeyboardInterrupt (CTRL+C). | |
try: | |
if get_last_mod() != last_modification: | |
# Has the `my_turtle.py` file been saved? | |
print('Reloading...') | |
last_modification = get_last_mod() | |
# Empty the window, start from scratch. | |
turtle.reset() | |
# Re-run the instructions in the `my_turtle.py` file. | |
with open('my_turtle.py', 'r') as my_turtle: | |
exec(my_turtle.read()) | |
time.sleep(1) # Poll once a second. | |
except KeyboardInterrupt: # CTRL+C in the terminal. | |
print('Bye!') | |
sys.exit() | |
except: | |
# Anything wrong in the `my_turtle.py` file? | |
# Don't fail, just display the traceback, and continue. | |
print(traceback.format_exc()) |
Oh, btw, if you'd rather have french instead of english, very easy, add the following few lines after line 30 for example:
tortue = turtle
turtle.avance = turtle.forward
turtle.droite = turtle.right
turtle.gauche = turtle.left
Now you can have the following in the my_turtle.py
file:
tortue.avance(100)
tortue.gauche(90)
tortue.avance(100)
tortue.gauche(90)
tortue.avance(100)
tortue.gauche(90)
tortue.avance(100)
Does inotifyrun python turtle.py
not fit your needs ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah, this is pretty horrible (
exec
really?)It's a few lines thrown together to improve the experience of users: saving their code in the
my_turtle.py
file will automatically reset the turtle window and rerun the commands in there.Example content for the
my_turtle.py
file:This lets you use just any editor (choose one that is easy enough, not sure that putting them in vim and teaching them "esc + :w" is a good idea).
It's also much nicer than the previous flow I had when showing the mighty turtle to non-developers:
1/ I type
import turtle\nturtle.forward(100)
in the REPL2/ I go find some unsuspecting victim and tell them "hey, how are you, look at this nice turtle, it's going to move"
3/ "your turn, try it with different values instead of 100"
4/ "ok, your turtle went off the screen, give me the keyboard back while I type
turtle.reset()
"5/ "now your turn, the computer is all yours (until I'll need to take it back to reset the window). Oh, by the way, you need to re-type everything from the beginning (yeah, even when you learn to write functions or loops). The 'up key' isn't going to help much. You'll just suffer. Also, the mouse won't help you much in the REPL. Or even not at all. Use the arrow keys."