Welcome to SIMPL Programming for Python
Lesson #1

Course content Enter chat room Send email
to mailing list
Check calendar

JUST A REMINDER: This course does not have a mailing list. If you need assistance you can email info[ATnospam]icanprogramDOTcom.

In memory of Linda. Worldwide Cancer sites here. Canadian Cancer donations here.

Hint:

All of these lessons are designed to be worked on off-line. For those of you with dial up Internet access you might find it helpful to print off the page for each lesson and work from the printout.

The basics

SIMPL is simultaneously a LGPL'd library, a programming framework and a toolkit.
SIMPL modules are themselves Linux processes.
SIMPL modules can be written in several languages. Python is one of them.
SIMPL applications consist of two or more interacting SIMPL modules.
SIMPL applications can contain a mixture of SIMPL modules written in any supported programming language.

Before proceeding, the following assumptions have been made:

  1. you are running a functional Linux distribution which supports C and Python programming,
  2. you have some familiarity with C programming,
  3. you have some familiarity with the Python language.

The self installing archive discussed in the Course Organization section in the lesson below will allow you to install all the additional tools you will need for these lessons in one simple procedure. Some of you will have been exposed to SIMPL in a previous iCanProgram course. However, it would be advantageous to review the SIMPL toolkit before proceding to the lesson below.

In addition the authors of this course have recently published a book on the SIMPL toolkit. That book includes several examples of using Python in SIMPL applications.

Programming the SIMPL Way


What is this course about?

This course is about extending the messaging power of SIMPL to Python scripts. In this way Python programs can communicate via SIMPL with other network connected programs that also support SIMPL. To date, SIMPL libraries/modules exist for programs written in C, Tcl, Java, and Python running on Linux, Unix, QNX, and MAC OS X platforms.

What is equally important is what this course is not about. It is not a course on how to program in Python. Nevertheless, we will go over certain aspects of Python as they relate directly to the usage of SIMPL.

Why Python?

We are not going to get into the debate of what languages are better than others and so on. It suffices to say that each language has its strengths and weaknesses much like any tool.

We recommend that you read the document called readme.python which is located in the docs directory below the SIMPL root directory. This doc contains a discussion of the reasoning, history and philosophy behind Python SIMPL.

We are using what is known as CPython. That is, Python written in C/C++ as opposed to JPython which has been written in Java. The code that you will use has been run successfully on versions Python1.5 through Python3.2.

There is a discontinuous upgrade path from Python 2.x series to Python 3.x series. Python scripts are not portable across that boundary. As such there are two separate Python-SIMPL libraries as well. While both libraries will be maintained near term going forward, we would encourage you to use the Python3 codebase for all your SIMPL projects.

For graphics capability, we are using the Tk toolbox via Python's Tkinter module.

What are we going to be doing throughout this course?

Necessarily, we are going to run and examine some programs. The aim is to show how the following pairs of programs operate:


Lesson #2 - python text-based sender and python text-based receiver,



Lesson #3 - python text-based sender and C receiver,



Lesson #4 - python graphics-based sender and C receiver,



Lesson #5 - C sender and python graphics-based receiver,



Lesson #6 - python graphics-based sender, tclSurrogate and C receiver,



Lesson #7 - browser to server (HTML form) calling CGI python sender to C receiver.


Python text-based and python graphics-based refer the user interface of the python scripts.

All of the programs have been written for you but you are encouraged to modify them and experiment.

We will be promoting the concept of tokenized message passing in all cases.

There are exactly three different messages that are sent and received throughout all of the lessons. Each message uses a token as an identifier.

The messages are kept the same throughout all of the lessons in order to maintain some continuity. Each message contains different data types in order to demonstrate how these various data types may be used to exchange information between the various senders and receivers.

There is a certain amount of repetition of the programs between the various lessons. This can be viewed as an example of the versatlility of SIMPL programs in the sense that any SIMPL program can communicate with any other SIMPL program on any other supported platform and written in any other SIMPL supported language.

Course Organization

All the code you will be using in this course can be downloaded as a tarball from here.

Note:

Not all Python 3.x installs are the same. On many systems both Python2.x and Python3.x co-exist. Care must be taken to insure that the corrrect Python-SIMPL shared library is built before proceding.

This set of courses is organized in the following hierarchical directory structure: (Note that /.../ in the directory path is where you have undone your tar ball.)
/... /python-simpl-course/src
/.../python-simpl-course/bin

All of the C source code and Python scripts in the src directory.
All of the C executables will reside in the bin directory.

Necessary System Items

  1. It is imperative that the SIMPL_HOME environment variable contains the path to the SIMPL distribution you are using. The Makefile that is used to build your C executables will not function without it. If you have done another SIMPL course then you should already have this set.
  2. It is also helpful that the Python interpreter is in your PATH environment variable. This is the default on most Linux systems.
  3. It is necessary that you set the PYTHONPATH environment variable to $(SIMPL_HOME)/python/modules. This allows the Python interpreter to find the python-simpl modules that come with your SIMPL distribution.

A Short Overview of Python

At this point we are going to become acquainted with the parts of the Python language that are necessary for us to accomplish our task of showing how SIMPL may be used in conjunction with Python.

First of all, Python is an interpreted scripting language. This is to say that a Python program is a script written similarly to a shell script.

The script is read by the Python interpreter in the same way that BASH will read a shell script. The Python interpreter is called python and should be found in a directory in your command path, PATH.

To find out where it is, type: "which python" at the command prompt (without the quotes of course). If you get a null result, then you will have to make sure that python has been loaded onto your system and perhaps add its location to your PATH environment variable.

To run a Python script called myProgram.py type: "python3 myProgram.py" at the command prompt, as always without the quotes.

The interpreter will read the script file called myProgram.py and compile it into a byte code file called myProgram.pyc. It is actually myProgram.pyc that gets run by the interpreter. This is good for many reasons because it allows you to keep your original script from prying eyes and simply release your byte code file in a way that is similar to releasing executable binary code rather than source.

What would a typical myProgram.py file look like? Basically like the following:

Note:

This is not to be run; it is an example only.

"""
This is a header to the script which contains any relevant information you wish to convey. Multiple line comments like this can be started and ended with triple quotes.
"""

# blank lines are ignored. single line comments are started with a # just like as in shell scripts

# at this point we will import any needed functionality from other python scripts that come with the distribution
import myFunctions

# now we can define one of our own functions. Note that the use of indents signal logical code groupings in the same way that C uses brace brackets
def myFunc():
print ("This is my function")

# now we put in the body of the code logic
print ("Let's start this program")

# let's call our defined function
myFunc()

A pretty useless script right? To be sure. Nevertheless, this is a basic layout for a Python program and all of our lesson scripts will follow this pattern.

Summary

Let's summarize what you have learned in this lesson.

End of Lesson 1.


Copyright of iCanProgram Inc. 2012