Python

From Center for Cognitive Neuroscience
Jump to navigation Jump to search

Python

Python is the official scripting language of the lab. Anything beyond trivial work should use python to ensure homogeneity, interoperability, and future use of that work.

Experienced in Programming, New to Python

If you're already an experienced programmer, but new to Python. The free online book Dive Into Python is right up your alley. It only lightly focuses on basic controllers like iterators, loops, conditional branching (if/else), and common data structures (arrays, hashes, etc). It focuses on specific Python idioms and linguistic sugar to get you from 0 to coding in as short a time as possible.

Please note the book was written for Python 2.2, so some of the behavior may vary slightly from that given in the book. For example, dir() lists both built-ins and public methods rather than just public methods. However, the differences aren't that significant and it still servers well as a quickie introduction.

New to Programming

If you're new to programming in general, the Think Python free online book was written with you in mind. It's a gentle introduction to Object Oriented design principles...the python way. Don't worry, if you don't know what that means this manual will teach you.

Standard Documentation

Finally, there are the standard Python resources of Official Python Tutorial, The Semi-Official Python FAQ, and Python.org.






FAQ

How do I configure my editor?

It is very important that all collaborators maintain the same editing preferences for projects when using Python. This ensures that your time will be spent programming and not debugging the often hard to pin down indentation level syntax errors that can result from having different editor settings between users.

Configuring your editor to conform to the following PEP recommendations is required and highly recommended in general

  • shiftwidth = 4
  • tabstop = 8
  • expanded tabs (replace tab with spaces)
  • wrap text after 79 columns to ensure that it does not exceed 80 columns (this is canonical programming practice in general)
  • Use unix end of line \n
  • set file encoding to UTF-8 (recommended, not required)

How do I update a matplotlib in Wingware IDE?

The answer to this question was provided by Stephan Deibel of Wingware IDE support. As of this writing, this method has only been tested in windows. For OSX, it's best to use the ipython shell with -pylab switch.

Users of matplotlib often work interactively in the Python command
line shell.  For example, two plots could be shown in succession
as follows::

 from pylab import plot,show,close
 x = range(10)
 plot(x)
 show() # This blocks until the plot window is closed manually
 y = [2, 8, 3, 9, 4]
 plot(y)
 close()

When this is done in Wing's Python Shell, the show() will not return until the
first window is closed and subsequent plots will not run interactively so
their windows will fail to update if obscured and re-displayed. This happens
because the main loop that services events for the plot windows is not being
run.

To solve this problem, try using a ``Timer`` from Python's threading module for
the ``show()`` call, as follows::
 
 from threading import Timer
 t = Timer(0, show)
 t.start()

This has been tested on Windows with numpy 1.2.1, scipy 0.7.0.b1, and a
matplotlib 0.98.5.2 installation where ``matplotlib.get_backend()`` reports
"TkAgg".  It may not work with other backends or on other OSes, although
it may be possible to devise a similar technique for other backends by
calling the GUI layer directly.

Useful Python Programs

Komodo Edit

A free and open source editor based on the award-winning Komodo IDE. You can find Komodo Edit at | ActiveState. They also offer the outstanding, but not free, Komodo IDE for a full fledged multi-lingual development environment.

Wing IDE

The | Wingware IDE is a great Python interactive development environment. It works well for general projects as well as SciPy focused projects. It features all of the things one would expect in an IDE such as code completion, project management, a debugger, built-in shell (like the matlab terminal), syntax highlighting, and much more. Please see the Wingware | Tutorials, | HowTo's, and | Reference Manual to get started. If you run into any issues and can't find the answer in the documentation, there's the | mailing lists, and | Priority Support (mostly for paying customers).

Wingware provides two versions of IDE. The | Professional, | Personal, and the | 101 versions.

The 101 version is free to everyone, but is limited in its features. The Personal is $35 dollars and the Professional weighs in at $95 (education pricing).

Free Wingware Professional

Wingware has an | amazing deal that allows one to obtain the Professional version for free with no limitations. The only requirement is that you write a useful, non-trivial program and license it under an | OSI Approved license. This, for most people, means the GPL or BSD licenses.

SciPy

| SciPy is mathematical suite for Python. It provides the same functionality as matlab, including plotting. As of yet, I have not found a function or ability present in matlab that does not have a corollary in SciPy. If you do, please let me know.