summaryrefslogtreecommitdiff
path: root/Lib/turtle.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/turtle.py')
-rw-r--r--Lib/turtle.py194
1 files changed, 96 insertions, 98 deletions
diff --git a/Lib/turtle.py b/Lib/turtle.py
index 0c439f1e3b..18529a151d 100644
--- a/Lib/turtle.py
+++ b/Lib/turtle.py
@@ -27,10 +27,10 @@ Turtle graphics is a popular way for introducing programming to
kids. It was part of the original Logo programming language developed
by Wally Feurzig and Seymour Papert in 1966.
-Imagine a robotic turtle starting at (0, 0) in the x-y plane. Give it
+Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an ``import turtle``, give it
the command turtle.forward(15), and it moves (on-screen!) 15 pixels in
the direction it is facing, drawing a line as it moves. Give it the
-command turtle.left(25), and it rotates in-place 25 degrees clockwise.
+command turtle.right(25), and it rotates in-place 25 degrees clockwise.
By combining together these and similar commands, intricate shapes and
pictures can easily be drawn.
@@ -96,7 +96,7 @@ Roughly it has the following features added:
docstrings to disc, so it can serve as a template for translations.
Behind the scenes there are some features included with possible
-extensions in in mind. These will be commented and documented elsewhere.
+extensions in mind. These will be commented and documented elsewhere.
"""
@@ -109,6 +109,7 @@ import types
import math
import time
import os
+import inspect
from os.path import isfile, split, join
from copy import deepcopy
@@ -169,9 +170,8 @@ _CFG = {"width" : 0.5, # Screen
def config_dict(filename):
"""Convert content of config-file into dictionary."""
- f = open(filename, "r")
- cfglines = f.readlines()
- f.close()
+ with open(filename, "r") as f:
+ cfglines = f.readlines()
cfgdict = {}
for line in cfglines:
line = line.strip()
@@ -857,8 +857,8 @@ class TurtleScreenBase(object):
class Terminator (Exception):
"""Will be raised in TurtleScreen.update, if _RUNNING becomes False.
- Thus stops execution of turtle graphics script. Main purpose: use in
- in the Demo-Viewer turtle.Demo.py.
+ This stops execution of a turtle graphics script.
+ Main purpose: use in the Demo-Viewer turtle.Demo.py.
"""
pass
@@ -905,7 +905,7 @@ class Shape(object):
>>> poly = ((0,0),(10,-5),(0,10),(-10,-5))
>>> s = Shape("compound")
>>> s.addcomponent(poly, "red", "blue")
- ### .. add more components and then use register_shape()
+ >>> # .. add more components and then use register_shape()
"""
if self._type != "compound":
raise TurtleGraphicsError("Cannot add component to %s Shape"
@@ -1003,7 +1003,7 @@ class TurtleScreen(TurtleScreenBase):
no backgroundimage, no eventbindings and tracing on.
Example (for a TurtleScreen instance named screen):
- screen.clear()
+ >>> screen.clear()
Note: this method is not available as function.
"""
@@ -1077,8 +1077,8 @@ class TurtleScreen(TurtleScreenBase):
Example (for a TurtleScreen instance named screen):
>>> screen.setworldcoordinates(-10,-0.5,50,1.5)
>>> for _ in range(36):
- left(10)
- forward(0.5)
+ ... left(10)
+ ... forward(0.5)
"""
if self.mode() != "world":
self.mode("world")
@@ -1182,7 +1182,7 @@ class TurtleScreen(TurtleScreenBase):
>>> screen.colormode()
1.0
>>> screen.colormode(255)
- >>> turtle.pencolor(240,160,80)
+ >>> pencolor(240,160,80)
"""
if cmode is None:
return self._colormode
@@ -1250,9 +1250,9 @@ class TurtleScreen(TurtleScreenBase):
>>> screen.tracer(8, 25)
>>> dist = 2
>>> for i in range(200):
- fd(dist)
- rt(90)
- dist += 2
+ ... fd(dist)
+ ... rt(90)
+ ... dist += 2
"""
if n is None:
return self._tracing
@@ -1279,7 +1279,7 @@ class TurtleScreen(TurtleScreenBase):
self._delayvalue = int(delay)
def _incrementudc(self):
- "Increment upadate counter."""
+ """Increment upadate counter."""
if not TurtleScreen._RUNNING:
TurtleScreen._RUNNNING = True
raise Terminator
@@ -1347,16 +1347,12 @@ class TurtleScreen(TurtleScreenBase):
clicked point on the canvas.
num -- the number of the mouse-button, defaults to 1
- Example (for a TurtleScreen instance named screen
- and a Turtle instance named turtle):
-
- >>> screen.onclick(turtle.goto)
+ Example (for a TurtleScreen instance named screen)
- ### Subsequently clicking into the TurtleScreen will
- ### make the turtle move to the clicked point.
+ >>> screen.onclick(goto)
+ >>> # Subsequently clicking into the TurtleScreen will
+ >>> # make the turtle move to the clicked point.
>>> screen.onclick(None)
-
- ### event-binding will be removed
"""
self._onscreenclick(fun, btn, add)
@@ -1370,20 +1366,18 @@ class TurtleScreen(TurtleScreenBase):
In order to be able to register key-events, TurtleScreen
must have focus. (See method listen.)
- Example (for a TurtleScreen instance named screen
- and a Turtle instance named turtle):
+ Example (for a TurtleScreen instance named screen):
>>> def f():
- fd(50)
- lt(60)
-
-
+ ... fd(50)
+ ... lt(60)
+ ...
>>> screen.onkey(f, "Up")
>>> screen.listen()
- ### Subsequently the turtle can be moved by
- ### repeatedly pressing the up-arrow key,
- ### consequently drawing a hexagon
+ Subsequently the turtle can be moved by repeatedly pressing
+ the up-arrow key, consequently drawing a hexagon
+
"""
if fun is None:
if key in self._keys:
@@ -1407,16 +1401,15 @@ class TurtleScreen(TurtleScreenBase):
and a Turtle instance named turtle):
>>> def f():
- fd(50)
-
-
- >>> screen.onkey(f, "Up")
+ ... fd(50)
+ ... lt(60)
+ ...
+ >>> screen.onkeypress(f, "Up")
>>> screen.listen()
- ### Subsequently the turtle can be moved by
- ### repeatedly pressing the up-arrow key,
- ### or by keeping pressed the up-arrow key.
- ### consequently drawing a hexagon.
+ Subsequently the turtle can be moved by repeatedly pressing
+ the up-arrow key, or by keeping pressed the up-arrow key.
+ consequently drawing a hexagon.
"""
if fun is None:
if key in self._keys:
@@ -1448,12 +1441,12 @@ class TurtleScreen(TurtleScreenBase):
>>> running = True
>>> def f():
- if running:
- fd(50)
- lt(60)
- screen.ontimer(f, 250)
-
- >>> f() ### makes the turtle marching around
+ ... if running:
+ ... fd(50)
+ ... lt(60)
+ ... screen.ontimer(f, 250)
+ ...
+ >>> f() # makes the turtle marching around
>>> running = False
"""
self._ontimer(fun, t)
@@ -1497,7 +1490,7 @@ class TurtleScreen(TurtleScreenBase):
Example (for a Turtle instance named turtle):
>>> turtle.screensize(2000,1500)
- ### e. g. to search for an erroneously escaped turtle ;-)
+ >>> # e.g. to search for an erroneously escaped turtle ;-)
"""
return self._resize(canvwidth, canvheight, bg)
@@ -2085,7 +2078,7 @@ class TPen(object):
Example (for a Turtle instance named turtle):
>>> turtle.pensize()
1
- turtle.pensize(10) # from here on lines of width 10 are drawn
+ >>> turtle.pensize(10) # from here on lines of width 10 are drawn
"""
if width is None:
return self._pensize
@@ -2560,7 +2553,7 @@ class RawTurtle(TPen, TNavigator):
"""Delete the turtle's drawings and restore its default values.
No argument.
-,
+
Delete the turtle's drawings from the screen, re-center the turtle
and set variables to the default values.
@@ -2607,7 +2600,7 @@ class RawTurtle(TPen, TNavigator):
Example (for a Turtle instance named turtle):
>>> while undobufferentries():
- undo()
+ ... undo()
"""
if self.undobuffer is None:
return 0
@@ -2683,9 +2676,9 @@ class RawTurtle(TPen, TNavigator):
>>> turtle.tracer(8, 25)
>>> dist = 2
>>> for i in range(200):
- turtle.fd(dist)
- turtle.rt(90)
- dist += 2
+ ... turtle.fd(dist)
+ ... turtle.rt(90)
+ ... dist += 2
"""
return self.screen.tracer(flag, delay)
@@ -2883,7 +2876,6 @@ class RawTurtle(TPen, TNavigator):
>>> turtle.shapesize(5,2)
>>> turtle.tilt(45)
>>> turtle.tiltangle()
- >>>
"""
if angle is None:
tilt = -self._tilt * (180.0/math.pi) * self._angleOrient
@@ -2928,7 +2920,7 @@ class RawTurtle(TPen, TNavigator):
>>> turtle.shapesize(4,2)
>>> turtle.shearfactor(-0.5)
>>> turtle.shapetransform()
- >>> (4.0, -1.0, -0.0, 2.0)
+ (4.0, -1.0, -0.0, 2.0)
"""
if t11 is t12 is t21 is t22 is None:
return self._shapetrafo
@@ -3126,7 +3118,7 @@ class RawTurtle(TPen, TNavigator):
Example (for a Turtle instance named turtle):
>>> for i in range(8):
- turtle.stamp(); turtle.fd(30)
+ ... turtle.stamp(); turtle.fd(30)
...
>>> turtle.clearstamps(2)
>>> turtle.clearstamps(-2)
@@ -3147,7 +3139,7 @@ class RawTurtle(TPen, TNavigator):
if pen is down. All other methodes for turtle movement depend
on this one.
"""
- ## Version mit undo-stuff
+ ## Version with undo-stuff
go_modes = ( self._drawing,
self._pencolor,
self._pensize,
@@ -3302,9 +3294,9 @@ class RawTurtle(TPen, TNavigator):
Example (for a Turtle instance named turtle):
>>> turtle.begin_fill()
>>> if turtle.filling():
- turtle.pensize(5)
- else:
- turtle.pensize(3)
+ ... turtle.pensize(5)
+ ... else:
+ ... turtle.pensize(3)
"""
return isinstance(self._fillpath, list)
@@ -3534,9 +3526,9 @@ class RawTurtle(TPen, TNavigator):
Example for the anonymous turtle, i. e. the procedural way:
>>> def turn(x, y):
- left(360)
-
- >>> onclick(turn) # Now clicking into the turtle will turn it.
+ ... left(360)
+ ...
+ >>> onclick(turn) # Now clicking into the turtle will turn it.
>>> onclick(None) # event-binding will be removed
"""
self.screen._onclick(self.turtle._item, fun, btn, add)
@@ -3552,16 +3544,17 @@ class RawTurtle(TPen, TNavigator):
Example (for a MyTurtle instance named joe):
>>> class MyTurtle(Turtle):
- def glow(self,x,y):
- self.fillcolor("red")
- def unglow(self,x,y):
- self.fillcolor("")
-
+ ... def glow(self,x,y):
+ ... self.fillcolor("red")
+ ... def unglow(self,x,y):
+ ... self.fillcolor("")
+ ...
>>> joe = MyTurtle()
>>> joe.onclick(joe.glow)
>>> joe.onrelease(joe.unglow)
- ### clicking on joe turns fillcolor red,
- ### unclicking turns it to transparent.
+
+ Clicking on joe turns fillcolor red, unclicking turns it to
+ transparent.
"""
self.screen._onrelease(self.turtle._item, fun, btn, add)
self._update()
@@ -3580,9 +3573,9 @@ class RawTurtle(TPen, TNavigator):
Example (for a Turtle instance named turtle):
>>> turtle.ondrag(turtle.goto)
- ### Subsequently clicking and dragging a Turtle will
- ### move it across the screen thereby producing handdrawings
- ### (if pen is down).
+ Subsequently clicking and dragging a Turtle will move it
+ across the screen thereby producing handdrawings (if pen is
+ down).
"""
self.screen._ondrag(self.turtle._item, fun, btn, add)
@@ -3630,10 +3623,11 @@ class RawTurtle(TPen, TNavigator):
Example (for a Turtle instance named turtle):
>>> for i in range(4):
- turtle.fd(50); turtle.lt(80)
-
+ ... turtle.fd(50); turtle.lt(80)
+ ...
>>> for i in range(8):
- turtle.undo()
+ ... turtle.undo()
+ ...
"""
if self.undobuffer is None:
return
@@ -3893,31 +3887,35 @@ except:
def getmethparlist(ob):
- "Get strings describing the arguments for the given object"
- argText1 = argText2 = ""
+ """Get strings describing the arguments for the given object
+
+ Returns a pair of strings representing function parameter lists
+ including parenthesis. The first string is suitable for use in
+ function definition and the second is suitable for use in function
+ call. The "self" parameter is not included.
+ """
+ defText = callText = ""
# bit of a hack for methods - turn it into a function
# but we drop the "self" param.
# Try and build one for Python defined functions
- argOffset = 1
- counter = ob.__code__.co_argcount
- items2 = list(ob.__code__.co_varnames[argOffset:counter])
- realArgs = ob.__code__.co_varnames[argOffset:counter]
+ args, varargs, varkw = inspect.getargs(ob.__code__)
+ items2 = args[1:]
+ realArgs = args[1:]
defaults = ob.__defaults__ or []
- defaults = list(map(lambda name: "=%s" % repr(name), defaults))
+ defaults = ["=%r" % (value,) for value in defaults]
defaults = [""] * (len(realArgs)-len(defaults)) + defaults
- items1 = list(map(lambda arg, dflt: arg+dflt, realArgs, defaults))
- if ob.__code__.co_flags & 0x4:
- items1.append("*"+ob.__code__.co_varnames[counter])
- items2.append("*"+ob.__code__.co_varnames[counter])
- counter += 1
- if ob.__code__.co_flags & 0x8:
- items1.append("**"+ob.__code__.co_varnames[counter])
- items2.append("**"+ob.__code__.co_varnames[counter])
- argText1 = ", ".join(items1)
- argText1 = "(%s)" % argText1
- argText2 = ", ".join(items2)
- argText2 = "(%s)" % argText2
- return argText1, argText2
+ items1 = [arg + dflt for arg, dflt in zip(realArgs, defaults)]
+ if varargs is not None:
+ items1.append("*" + varargs)
+ items2.append("*" + varargs)
+ if varkw is not None:
+ items1.append("**" + varkw)
+ items2.append("**" + varkw)
+ defText = ", ".join(items1)
+ defText = "(%s)" % defText
+ callText = ", ".join(items2)
+ callText = "(%s)" % callText
+ return defText, callText
def _turtle_docrevise(docstr):
"""To reduce docstrings from RawTurtle class for functions