Chapter 7. Python Scripting

Table of Contents

Starting a Python Session
Graphical Python Consoles
Command-Line Sessions
Script Packets
Accessing Regina from Python
Documentation
Caveats and Traps
Uneditable Packets
International and Special Characters
Ownership Issues and Boost.Python.ArgumentError / TypeError
Sample Python Sessions
Working with a triangulation
Working with a packet tree
Reporting progress of long operations

Regina contains inbuilt scripting, which offers you fine control over Regina's mathematical engine and enables you to automate large amounts of processing. You can write and run scripts from directly within Regina, or you can use the separate command-line tool regina-python.

The user interface will stay in sync with any changes that you make through a script, which means you can happily modify a data file via scripts while you have it open.

All scripts in Regina use the Python programming language (http://www.python.org/).

Starting a Python Session

There are several ways of starting a Python session to work with Regina:

Graphical Python Consoles

You can open a graphical Python console by selecting ToolsPython Console from the menu (or by pressing the corresponding toolbar button).

A new console window will open as illustrated below, with an input area down the bottom (see the red arrow) and a full history of the session in the main part of the window. You can save this history by selecting ConsoleSave Session.

When you start the Python session, Regina will set some special variables for you:

root

If you have a data file open, the variable root will refer to the packet at the top of your packet tree.

selected

If you have a packet selected in the tree, the variable selected will refer to this packet (see the figure above for an example of this).

You are welcome to use these variables to change packets in your data file (or even add, remove or rename packets): the graphical user interface will always stay in sync with any changes that you make via Python.

Command-Line Sessions

You can run the command-line program regina-python without a graphical user interface at all. This will use the standard Python interpreter. Since this is a text-based interface, you can also redirect input and output in the usual way (using < and > in your command shell).

Script Packets

You can create a new script packet in your data file. Script packets allow you to save Python scripts along with your data, and they give your scripts easy access to the packets inside your file.

When you open a script packet, you will see your Python code in the lower part of the script editor, and a table of variables up the top.

You can add your own variables to this table, and set them to arbitrary packets within your data file. Regina will always set these variables to the corresponding packets before running your script.

At the top of the script editor you will find buttons to compile and run your script. Compiling is optional: it merely gives you a chance to spot syntax errors as you go. When you press Run, Regina will run your script in a new graphical Python console. The console will be left open in case you wish to experiment further.

Again, you are welcome to change packets in your data file via scripts (or even add, remove or rename packets): the graphical user interface will always stay in sync with any changes that your scripts make.