Tuesday, October 19, 2010

Getting started with Python

Official web site: http://python.org/
This tutorial is really cool: http://docs.python.org/tutorial
Reference: http://python.org/doc/

A dynamic language. Interpreted not compiled. Sort of C-like syntax, close enough that certainly I felt comfortable in it. Also used as a scripting language. Easy to learn.

Like C .... except the whitespace is significant in front of expressions: so no braces { } Don't mix tabs and spaces before statements because you'll get it wrong. Intent consistently and you'll be fine. It appears most people love the idea (everything is correctly formatted!). Then hate it when they start. Then love it once they've been doing it for a while.

Here is some Python 2.x code (they are currently working on Python 2.x and 3.x in parallel).

def hello():
print "hello"


Summary: Define a function. Print hello. Call the function. Notice the tab/spaces before print.

Other features:
  • Lists are sort of like C arrays, but more powerful. They use array-style syntax. These are the general purpose container of Python and get used all over the place to store stuff. Pretty efficient (e.g. faster to add characters to a list than a string). sid[3] = 5
  • There is a big library that does pretty much everything. Send emails, get web pages, regex, web servers, etc. etc.
  • Types are inferred rather than specified explicitly. 'Duck' typing. Strong typing.
  • It's an OO language. Naturally. But also supports imperative (procedural) and to a lesser extent functional programming.
  • Automatic memory management
  • There are dictionary which do map-style key-value look up. Used occasionally.
  • It's in the same class of languages (dynamic, interpreted, scripting) as Perl, Ruby, Tcl, PHP, etc.
  • In my opinion: Fast to program. As fast (or faster) than BASIC, and probably > 4x faster to program than C or C++ for the general case.
  • Interpreted = sometimes slow to run. But we have fast computers, so that doesn't matter.


We are using Python 2.x because that's what one of the interpreters that comes by default with Mac OS X, although for the work here you can use any operating system.

We are using to run a couple of things for our current weather program, which talks to hardware weather stations.


Specially, I'm writing a file converter - from other formats to LWC format.

(We've also got web page scrapers, called 'Decoders' written in Python. They need to be easy to change, hence Python.)

The Python Interpreter

On Mac OS X you'll have Python installed. On Windows you'll need to install it: http://python.org/download/
Some Linux and BSD distribution have it installed - otherwise that link will do it as well.

MacOSX Version = Python Version
10.3 = 2.3.0
10.4 = 2.3.5
10.5 = 2.5.1
10.6 = 2.6.1 (typing python2.5 gets you python2.5.4)

For MacOSX, if you go to Terminal (that's in Application/Utilities if it's not in your doc) and type 'python --version' then it will show you the Python version installed. You can do a similar thing in Windows from the Command Prompt.

I've installed Python 2.6.6 for reasons I can't remember. It's unlikely that this is critically important and probably relates to some unrelated work I was doing. Yeah, I think it was PyGame stuff.

The Editor and Execution environment - ECLIPSE

You can edit python files (.py) in anything - they are straight text files. For instance on Windows Visual Studio works (but it doesn't know how to chroma code it by default...) and on MacOSX Xcode knows how to chroma code Python files.

You can run the code in the Terminal by typing 'python file.py' or 'file.py' (depending on various things).

However, both of these, whilst functional, suck slightly in various ways.

So I've started editing with Eclipse. http://www.eclipse.org/

This started life as a Java development environment, but I've used it elsewhere for embedded C/C++ and I use it also for Python development.

Get it here: http://www.eclipse.org/downloads/

It doesn't really matter what version you download - I have the 'Eclipse IDE for C/C++ Developers' installed (for various reasons), so I'd suggest that one.

Once you boot up Eclipse, you'll need to create a workspace. Create this somewhere like your Document folder. A workspace is a place to work (duh!) that contains, usually, multiple related projects and the settings for the whole environment (for that particular workspace). The actual project may or may not be inside the workspace folder.


There are a few ways of getting Python to work with Eclipse. I'm using http://pydev.org/

The basic instruction are, from within Eclipse... (see http://pydev.org/manual_101_install.html for screenshots of this process)

1. Go to the Help Menu
2. Go to 'Install New Software...'
3. Click Add next to 'Work with' .... add 'PyDev' and 'http://pydev.org/updates'
4. Follow the Instructions on that link above... (yeah, tired of typing :-D)

Create a Python Project

File -> New -> Pydev Project

Project name: test_project
you might need to configure a python interpreter...
Auto config should work...
If not ... New... Python2.5 then (on MacOSX) browse to /System/Library/Frameworks/Python.framework/Versions/2.x/Resources/Python.app/Contents/MacOS/Python and on Windows it will be in your Program Files folder.

Create '.py' file inside the project directory.

Type this into it....

def main():
print "Hello"


Right click on the file Run As... Python Run.
It should show 'Hello' in the Console window down the bottom.

You should also be able to put Debug As... Python Run.
Might help if you set a break-point.

(If you go into debugger view, you can click the 'PyDev' button on the top-right to switch back to editor mode.)



Hopefully at this point you'll have a working Python, Eclipse IDE and PyDev. You can create Python projects, run them and debug them.
Newer›  ‹Older