wiki:DevelopmentPage/PythonLanguage

Version 1 (modified by Michel Herquet, 14 years ago) ( diff )

--

Install Python

We develop our code using Python 2.6, which is available for all platforms on http://www.python.org/download/. Installation of binaries is straightforward.

Do not use another version, e.g. coming with Mac OSX or with your linux distribution. Those are often older an might not have all the required built-in functions.

Do not use Python 3.X since the built-in libraries are still incomplete. We will move to 3.X when the Python project will recommend to do so.

Comment: According to Pavel, RHEL5 will be the official Linux distribution at CERN for the next 3 years for the GRID machines. It appears it only comes with very old Python packages, essentially 2.4. This is an issue that should be discussed.

Optional: install matplotlib and scipy

By default, we will (a priori) not require the user to have those tools installed to run MG5, but those might be useful for developing purposes.

Scipy is necessary for all numerical calculations in python. You can find easy install instructions for all platforms on http://www.scipy.org/Installing_SciPy. You probably only need the numpy part of scipy.

Matplotlib is necessary for plotting histograms, diagrams, ... You can find binaries for your platform on http://sourceforge.net/projects/matplotlib/.

Please do not install a all-in-one python product (like Enthought for mac os), since the packages there are often not up-to-date.

Learn Python

The best way to quickly learn Python for people with a previous experience in programming is probably:

http://www.diveintopython.org/: a really nice free book, for advanced programmers, like you, who already know one language. Advanced topics like introspection, exceptions, unit tests, refactoring, functional programming, profiling, ... are nicely covered (with one concrete example in each case). The only problem: it has not been updated since 2004, but the language did not change that much since then. I think it's the best way to start.

There are probably nice Python books on Amazon but I never bought one.

For reference, the only relevant link is: http://docs.python.org/index.html

The PDF quick reference http://rgruet.free.fr/PQR26/PQR2.6_modern_a4.pdf could also deserve a free spot on your desk during the next months

For more advanced subjects:

How to integrate C routines: http://www.penzilla.net/tutorials/python/swig/

How to integrate F routines: for example: http://www.scipy.org/Cookbook/F2Py

A success story for scientific programming in Python : http://www.adass.org/adass/proceedings/adass99/O3-02/

Optional: learn Numpy and Matplotlib

For Numpy: you can learn to play with “arrays” (the basic object in Numpy) here: http://www.scipy.org/Tentative_NumPy_Tutorial

For Matplotlib: the simplest way is to look at the example gallery http://matplotlib.sourceforge.net/gallery.html to find something which looks like what you want, and to adapt the corresponding code.

Write PEP-8 compliant Python code

The PEP-8 document (http://python.org/dev/peps/pep-0008/) is often seen as the default format specification for Python. It is quite long to read, and almost impossible to remember. An easy way is to mimic a nicely formatted code. Print this one page PDF: http://wwd.ca/stuff/pep8_cheat.pdf and keep it close to your desktop, to see how your code should look like.

Note: See TracWiki for help on using the wiki.