Think Python/Case study: interface design

TurtleWorld
To accompany this book, I have written a suite of modules called Swampy. One of these modules is TurtleWorld, which provides a set of functions for drawing lines by steering turtles around the screen.

You can download Swampy from thinkpython.com/swampy; follow the instructions there to install Swampy on your system.

Move into the directory that contains TurtleWorld.py, create a file named polygon.py and type in the following code:

The first line is a variation of the import statement we saw before; instead of creating a module object, it imports the functions from the module directly, so you can access them without using dot notation.

The next lines create a TurtleWorld assigned to world and a Turtle assigned to bob. Printing bob yields something like:

This means that bob refers to an instance of a Turtle as defined in module <TT>TurtleWorld</TT>. In this context, "instance" means a member of a set; this Turtle is one of the set of possible Turtles.

tells TurtleWorld to wait for the user to do something, although in this case there's not much for the user to do except close the window.

TurtleWorld provides several turtle-steering functions: <TT>fd</TT> and <TT>bk</TT> for forward and backward, and <TT>lt</TT> and <TT>rt</TT> for left and right turns. Also, each Turtle is holding a pen, which is either down or up; if the pen is down, the Turtle leaves a trail when it moves. The functions <TT>pu</TT> and <TT>pd</TT> stand for &#X201C;pen up&#X201D; and &#X201C;pen down.&#X201D;

To draw a right angle, add these lines to the program (after creating <TT>bob</TT> and before calling ):

The first line tells <TT>bob</TT> to take 100 steps forward. The second line tells him to turn right.

When you run this program, you should see <TT>bob</TT> move east and then south, leaving two line segments behind.

Now modify the program to draw a square. Don&#X2019;t turn the page until you've got it working!

Simple repetition
Chances are you wrote something like this (leaving out the code that creates TurtleWorld and waits for the user): We can do the same thing more concisely with a <TT>for</TT> statement. Add this example to <TT>polygon.py</TT> and run it again:

You should see something like this: This is the simplest use of the <TT>for</TT> statement; we will see more later. But that should be enough to let you rewrite your square-drawing program. Don&#X2019;t turn the page until you do.

Here is a <TT>for</TT> statement that draws a square: The syntax of a <TT>for</TT> statement is similar to a function definition. It has a header that ends with a colon and an indented body. The body can contain any number of statements.

A <TT>for</TT> statement is sometimes called a loop because the flow of execution runs through the body and then loops back to the top. In this case, it runs the body four times.

This version is actually a little different from the previous square-drawing code because it makes another left turn after drawing the last side of the square. The extra turn takes a little more time, but it simplifies the code if we do the same thing every time through the loop. This version also has the effect of leaving the turtle back in the starting position, facing in the starting direction.

Exercises
The following is a series of exercises using TurtleWorld. They are meant to be fun, but they have a point, too. While you are working on them, think about what the point is.

The following sections have solutions to the exercises, so don&#X2019;t look until you have finished (or at least tried).

named <TT>t</TT>, which is a turtle. It should use the turtle to draw a square. Write a function call that passes <TT>bob</TT> as an argument to <TT>square</TT>, and then run the program again.
 * Write a function called <TT>square</TT> that takes a parameter

Modify the body so length of the sides is <TT>length</TT>, and then modify the function call to provide a second argument. Run the program again. Test your program with a range of values for <TT>length</TT>.
 * Add another parameter, named <TT>length</TT>, to <TT>square</TT>.

default, but you can provide a second argument that specifies the number of degrees. For example, <TT>lt(bob, 45)</TT> turns <TT>bob</TT> 45 degrees to the left. Make a copy of <TT>square</TT> and change the name to <TT>polygon</TT>. Add another parameter named <TT>n</TT> and modify the body so it draws an n-sided regular polygon. Hint: The angles of an n-sided regular polygon are 360.0 / <I>n</I> degrees.
 * The functions <TT>lt</TT> and <TT>rt</TT> make 90-degree turns by

and radius, <TT>r</TT>, as parameters and that draws an approximate circle by invoking <TT>polygon</TT> with an appropriate length and number of sides. Test your function with a range of values of <TT>r</TT>.
 * Write a function called <TT>circle</TT> that takes a turtle, <TT>t</TT>,

Hint: figure out the circumference of the circle and make sure that <TT>length * n = circumference</TT>.

Another hint: if <TT>bob</TT> is too slow for you, you can speed him up by changing <TT>bob.delay</TT>, which is the time between moves, in seconds. <TT>bob.delay = 0.01</TT> ought to get him moving.

that takes an additional parameter <TT>angle</TT>, which determines what fraction of a circle to draw. <TT>angle</TT> is in units of degrees, so when <TT>angle=360</TT>, <TT>arc</TT> should draw a complete circle.
 * Make a more general version of <TT>circle</TT> called <TT>arc</TT>

Encapsulation
The first exercise asks you to put your square-drawing code into a function definition and then call the function, passing the turtle as a parameter. Here is a solution:

The innermost statements, <TT>fd</TT> and <TT>lt</TT> are indented twice to show that they are inside the <TT>for</TT> loop, which is inside the function definition. The next line, <TT>square(bob)</TT>, is flush with the left margin, so that is the end of both the <TT>for</TT> loop and the function definition.

Inside the function, <TT>t</TT> refers to the same turtle <TT>bob</TT> refers to, so <TT>lt(t)</TT> has the same effect as <TT>lt(bob)</TT>. So why not call the parameter <TT>bob</TT>? The idea is that <TT>t</TT> can be any turtle, not just <TT>bob</TT>, so you could create a second turtle and pass it as an argument to <TT>square</TT>:

Wrapping a piece of code up in a function is called encapsulation. One of the benefits of encapsulation is that it attaches a name to the code, which serves as a kind of documentation. Another advantage is that if you re-use the code, it is more concise to call a function twice than to copy and paste the body!

Generalization
The next step is to add a <TT>length</TT> parameter to <TT>square</TT>. Here is a solution: Adding a parameter to a function is called generalization because it makes the function more general: in the previous version, the square is always the same size; in this version it can be any size.

The next step is also a generalization. Instead of drawing squares, <TT>polygon</TT> draws regular polygons with any number of sides. Here is a solution: This draws a 7-sided polygon with side length 70. If you have more than a few numeric arguments, it is easy to forget what they are, or what order they should be in. It is legal, and sometimes helpful, to include the names of the parameters in the argument list: These are called keyword arguments because they include the parameter names as &#X201C;keywords&#X201D; (not to be confused with Python keywords like <TT>while</TT> and <TT>def</TT>).

This syntax makes the program more readable. It is also a reminder about how arguments and parameters work: when you call a function, the arguments are assigned to the parameters.

Interface design
The next step is to write <TT>circle</TT>, which takes a radius, <TT>r</TT>, as a parameter. Here is a simple solution that uses <TT>polygon</TT> to draw a 50-sided polygon: The first line computes the circumference of a circle with radius <TT>r</TT> using the formula 2 &#X3C0; <I>r</I>. Since we use <TT>math.pi</TT>, we have to import <TT>math</TT>. By convention, <TT>import</TT> statements are usually at the beginning of the script.

<TT>n</TT> is the number of line segments in our approximation of a circle, so <TT>length</TT> is the length of each segment. Thus, <TT>polygon</TT> draws a 50-sides polygon that approximates a circle with radius <TT>r</TT>.

One limitation of this solution is that <TT>n</TT> is a constant, which means that for very big circles, the line segments are too long, and for small circles, we waste time drawing very small segments. One solution would be to generalize the function by taking <TT>n</TT> as a parameter. This would give the user (whoever calls <TT>circle</TT>) more control, but the interface would be less clean.

The interface of a function is a summary of how it is used: what are the parameters? What does the function do? And what is the return value? An interface is &#X201C;clean&#X201D; if it is &#X201C;as simple as possible, but not simpler. (Einstein)&#X201D;

In this example, <TT>r</TT> belongs in the interface because it specifies the circle to be drawn. <TT>n</TT> is less appropriate because it pertains to the details of how the circle should be rendered.

Rather than clutter up the interface, it is better to choose an appropriate value of <TT>n</TT> depending on <TT>circumference</TT>: Now the number of segments is (approximately) <TT>circumference/3</TT>, so the length of each segment is (approximately) 3, which is small enough that the circles look good, but big enough to be efficient, and appropriate for any size circle.

Refactoring
When I wrote <TT>circle</TT>, I was able to re-use <TT>polygon</TT> because a many-sided polygon is a good approximation of a circle. But <TT>arc</TT> is not as cooperative; we can&#X2019;t use <TT>polygon</TT> or <TT>circle</TT> to draw an arc.

One alternative is to start with a copy of <TT>polygon</TT> and transform it into <TT>arc</TT>. The result might look like this: The second half of this function looks like <TT>polygon</TT>, but we can&#X2019;t re-use <TT>polygon</TT> without changing the interface. We could generalize <TT>polygon</TT> to take an angle as a third argument, but then <TT>polygon</TT> would no longer be an appropriate name! Instead, let&#X2019;s call the more general function <TT>polyline</TT>: Now we can rewrite <TT>polygon</TT> and <TT>arc</TT> to use <TT>polyline</TT>: Finally, we can rewrite <TT>circle</TT> to use <TT>arc</TT>: This process&#X2014;rearranging a program to improve function interfaces and facilitate code re-use&#X2014;is called refactoring. In this case, we noticed that there was similar code in <TT>arc</TT> and <TT>polygon</TT>, so we &#X201C;factored it out&#X201D; into <TT>polyline</TT>.

If we had planned ahead, we might have written <TT>polyline</TT> first and avoided refactoring, but often you don&#X2019;t know enough at the beginning of a project to design all the interfaces. Once you start coding, you understand the problem better. Sometimes refactoring is a sign that you have learned something.

A development plan
A development plan is a process for writing programs. The process we used in this case study is &#X201C;encapsulation and generalization.&#X201D; The steps of this process are:


 * Start by writing a small program with no function definitions.

and give it a name.
 * Once you get the program working, encapsulate it in a function


 * Generalize the function by adding appropriate parameters.

Copy and paste working code to avoid retyping (and re-debugging).
 * Repeat steps 1&#X2013;3 until you have a set of working functions.

For example, if you have similar code in several places, consider factoring it into an appropriately general function.
 * Look for opportunities to improve the program by refactoring.

This process has some drawbacks&#X2014;we will see alternatives later&#X2014;but it can be useful if you don&#X2019;t know ahead of time how to divide the program into functions. This approach lets you design as you go along.

docstring
A docstring is a string at the beginning of a function that explains the interface (&#X201C;doc&#X201D; is short for &#X201C;documentation&#X201D;). Here is an example: This docstring is a triple-quoted string, also known as a multiline string because the triple quotes allow the string to span more than one line.

It is terse, but it contains the essential information someone would need to use this function. It explains concisely what the function does (without getting into the details of how it does it). It explains what effect each parameter has on the behavior of the function and what type each parameter should be (if it is not obvious).

Writing this kind of documentation is an important part of interface design. A well-designed interface should be simple to explain; if you are having a hard time explaining one of your functions, that might be a sign that the interface could be improved.

Debugging
An interface is like a contract between a function and a caller. The caller agrees to provide certain parameters and the function agrees to do certain work.

For example, <TT>polyline</TT> requires four arguments. The first has to be a Turtle (or some other object that works with <TT>fd</TT> and <TT>lt</TT>). The second has to be a number, and it should probably be positive, although it turns out that the function works even if it isn&#X2019;t. The third argument should be an integer; <TT>range</TT> complains otherwise (depending on which version of Python you are running). The fourth has to be a number, which is understood to be in degrees.

These requirements are called preconditions because they are supposed to be true before the function starts executing. Conversely, conditions at the end of the function are postconditions. Postconditions include the intended effect of the function (like drawing line segments) and any side effects (like moving the Turtle or making other changes in the World).

Preconditions are the responsibility of the caller. If the caller violates a (properly documented!) precondition and the function doesn&#X2019;t work correctly, the bug is in the caller, not the function. However, for purposes of debugging it is often a good idea for functions to check their preconditions rather than assume they are true. If every function checks its preconditions before starting, then if something goes wrong, you will know which function to blame.

Glossary
<DL CLASS="description"><DT CLASS="dt-description">instance:</DT><DD CLASS="dd-description"> A member of a set. The TurtleWorld in this chapter is a member of the set of TurtleWorlds. </DD><DT CLASS="dt-description">loop:</DT><DD CLASS="dd-description"> A part of a program that can execute repeatedly. </DD><DT CLASS="dt-description">encapsulation:</DT><DD CLASS="dd-description"> The process of transforming a sequence of statements into a function definition. </DD><DT CLASS="dt-description">generalization:</DT><DD CLASS="dd-description"> The process of replacing something unnecessarily specific (like a number) with something appropriately general (like a variable or parameter). </DD><DT CLASS="dt-description">keyword argument:</DT><DD CLASS="dd-description"> An argument that includes the name of the parameter as a &#X201C;keyword.&#X201D;

</DD><DT CLASS="dt-description">interface:</DT><DD CLASS="dd-description"> A description of how to use a function, including the name and descriptions of the arguments and return value. </DD><DT CLASS="dt-description">development plan:</DT><DD CLASS="dd-description"> A process for writing programs. </DD><DT CLASS="dt-description">docstring:</DT><DD CLASS="dd-description"> A string that appears in a function definition to document the function&#X2019;s interface. </DD><DT CLASS="dt-description">precondition:</DT><DD CLASS="dd-description"> A requirement that should be satisfied by the caller before a function starts. </DD><DT CLASS="dt-description">postcondition:</DT><DD CLASS="dd-description"> A requirement that should be satisfied by the function before it ends. </DD></DL>

Exercises
Exercise&#XA0;1&#XA0;&#XA0; Download the code in this chapter from '<TT>thinkpython.com/code/polygon.py</TT>'.


 * Write appropriate docstrings for '<TT>polygon</TT>', '<TT>arc</TT>' and '<TT>circle</TT>'.


 * Draw a stack diagram that shows the state of the program while executing '<TT>circle(bob, radius)</TT>'. You can do the arithmetic by hand or add '<TT>print</TT>' statements to the code.


 * The version of '<TT>arc</TT>' in Section&#XA0;'4.7' is not very accurate because the linear approximation of the circle is always outside the true circle. As a result, the turtle ends up a few units away from the correct destination. My solution shows a way to reduce the effect of this error. Read the code and see if it makes sense to you. If you draw a diagram, you might see how it works.

Exercise&#XA0;2&#XA0;&#XA0;

Write an appropriately general set of functions that can draw flowers like this:

<DIV CLASS="center"><IMG SRC="book005.png"></DIV>

You can download a solution from '<TT>thinkpython.com/code/flower.py</TT>'.

Exercise&#XA0;3&#XA0;&#XA0; Write an appropriately general set of functions that can draw shapes like this:

<DIV CLASS="center"><IMG SRC="book006.png"></DIV>

You can download a solution from '<TT>thinkpython.com/code/pie.py</TT>'.

<DIV CLASS="theorem">Exercise&#XA0;4&#XA0;&#XA0;

''The letters of the alphabet can be constructed from a moderate number of basic elements, like vertical and horizontal lines and a few curves. Design a font that can be drawn with a minimal number of basic elements and then write functions that draw letters of the alphabet.''

You should write one function for each letter, with names  ,  '', etc., and put your functions in a file named '<TT>letters.py</TT>'. You can download a &#X201C;turtle typewriter&#X201D; from '<TT>thinkpython.com/code/typewriter.py</TT>' to help you test your code.''

You can download a solution from '<TT>thinkpython.com/code/letters.py</TT>'.