Using Python

[TS: Still need heavy editing. This section probably is not needed since the target users at this point should be comfortable with Python.]

If you are asking "What's Python?", then take a moment to visit the About Python page. Briefly, Python is an object-oriented, highly dynamic "scripting" language created by Guido van Rossum.

First, a disclaimer: this is not a complete tutorial for Python. As a matter of fact, this is not even an incomplete tutorial for Python. This page is just going to cover enough so that you can use Python to write simple definitions for tioga figures and plots. At the end, some links are provided to places where you can get much more information.

Here is the "curve" procedure from "plots.py" that creates the "append_curve_to_path" plot shown above. Do not worry about understanding the details of what's going on (we will be discussing this routine later in the Simple Figure section). The discussion at this point is at the level of syntax and issues like what a procedure call looks like.

  1  def curve(t):
  2      t.landscape()
  3      t.show_text('append\_curve\_to\_path',
                     side=TOP, shift=0.6,
                     justification=Centered,
                     scale=0.9, position=0.5)
  4      background(t)
  5      x0 = 0.75; y0 = 0.9
  6      x1 = 0.9; y1 = 0.3
  7      x2 = 0.4; y2 = 0.1
  8      x3 = 0.1; y3 = 0.8
  9      t.move_to_point(x0, y0)
 10      t.append_curve_to_path(x1, y1, x2, y2, x3, y3)
 11      t.line_width = 2.5
 12      t.line_color = DarkBlue
 13      t.stroke()
 14      t.show_marker([ x0, x1, x2, x3 ],
                       [ y0, y1, y2, y3 ],
                       marker=Bullet,
                       scale=0.6,
                       color=Red)
 15      dx = t.default_text_height_dx * 1.4
 16      t.show_label(x=x0+dx, y=y0,
                      text='start', scale=0.9)
 17      scale = 1.2; dy = t.default_text_height_dy * 0.8
 18      t.show_marker(x1, y1+dy,
                       marker=Circled1, scale=scale)
 19      t.show_marker(x2, y2+dy,
                       marker=Circled2, scale=scale)
 20      t.show_marker(x3, y3+dy,
                       marker=Circled3, scale=scale)

The "t." stuff simply means we are talking to PyTioga, either to get values of variables (which in Python are called "attributes") or to get it to execute one of its procedures (which are called "methods"). There are cases in which we are setting attributes, such as t.line_width and t.line_color (lines 11 and 12), or using the values of attributes, such as t.default_text_height_dy (line 17).

There are a couple of obvious procedure calls to move_to_point() and append_curve_to_path() (lines 9 and 10). The "t.landscape()" on line 1 and the the "t.stroke()" on line 13 are procedure calls too even though they lack parentheses.

On lines 5 to 8, we have used semicolons to put two statements on a single line. Except for that special use, you can forget about semicolons. Square brackets are used for creating arrays (as in the calls to show_marker()).


Now that we are Python experts, let us look at a PyTioga figure definition — next stop: Simple Figure.