Difference between revisions of "Python"
(→Python on the Servers) |
|||
Line 4: | Line 4: | ||
====Python on the Servers==== | ====Python on the Servers==== | ||
+ | |||
+ | For now, be sure to read this [[http://www.mn.uio.no/geo/english/services/it/help/software/meteorology/python.html PYTHON]] guide from the Geoscience IT if you plan to work with the software. | ||
+ | |||
'''***IMPORTANT***''' | '''***IMPORTANT***''' | ||
Line 19: | Line 22: | ||
For more information see: | For more information see: | ||
* [[/SettingPaths]] | * [[/SettingPaths]] | ||
− | |||
==Resources== | ==Resources== |
Revision as of 13:23, 21 July 2014
The Python programming language is a powerful language which has gained popularity over the past several years. It is widely used in the scientific community, and there are numerous tools readily available for common data analysis tasks. At UiO there is a repository for Modules developed internally. The pages herein provide important information on working with Python on the UiO servers and also with your own PC.
Contents
Key Topics / Tips for Python at UiO
Python on the Servers
For now, be sure to read this [PYTHON] guide from the Geoscience IT if you plan to work with the software.
***IMPORTANT***
It is critical that you're display variable gets set properly when you ssh to the servers. In order to do this you need to make sure you connect with an ssh program that will forward your 'X' connections, on windows this means you'll need to have a X-server also running locally. XmingPortablePutty will take care of this for you. From linux it is simply a matter of:
ssh -XC user@remote
The '-XC' tells ssh to forward 'X' connections and to use 'C'ompression. For more details see JFBsWorkFlow
***IMPORTANT***
Once you have connected to the servers, in order to use Python it is important to know where the modules are, and how to set up your environment. Many of the commonly used modules are available already, and installed in a directory that you need to add to your PYTHONPATH
in order to use.
For more information see:
Resources
Best Practices
Let's start with some 'best practices' for programming with Python.
Code Style
First and foremost, one should become familiar with the PEP8. This is a 'Python Enhancement Proposal' (PEP). In Python this is the equivalent of a detailed featured request. PEP8 lays the foundation for how you should format your code.
One important feature of python to highlight are Documentation Strings:
Conventions for writing good documentation strings (a.k.a. "docstrings")
are immortalized in PEP 257 [3].
- Write docstrings for all public modules, functions, classes, and
methods. Docstrings are not necessary for non-public methods, but you
should have a comment that describes what the method does. This comment
should appear after the "def" line.
- PEP 257 describes good docstring conventions. Note that most
importantly, the """ that ends a multiline docstring should be on a line
by itself, and preferably preceded by a blank line, e.g.:
"""Return a foobang
Optional plotz says to frobnicate the bizbaz first.
"""
- For one liner docstrings, it's okay to keep the closing """ on the same
line.
Import Statements & Namespaces
In python, almost all code will be preceded with some statements such as: import this
(try that, by the way). These are 'import statements' which pull in other modules to use within your own code.
You may find some examples, particularly when learning matplotlib and working with 'pylab', where the tutorial uses:
from pylab import *
. This is NOT recommended. This will contaminate your local namespace, meaning that the module from which you import everything indicated by the '*', may overwrite some of your own functions or even python builtins.
The RECOMMENDED way to import modules is to:
import pylab as plb
Then you can still use the example, but you may simply need to prefix some of the function calls with 'plb.', which refers to the 'pylab' namespace.
Specific for Matplotlib
There will be some pages dedicated to Matplotlib, but in the meantime, the 'best practice' for working with Matplotlib is to use the 'object oriented' approach:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(100)
y = np.sin(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y)
plt.show()
Here we have shown an example of both the import of matplotlib.pyplot into the 'plt.' namespace, and numpy into the 'np.' namespace. For code at NILU this is the recommended import style.
Particularly for modules you intend to develop at NILU, this is important.
Useful Software / Modules
- /Sphinx
- /Numpy
- /Scipy
- /Matplotlib
- /Ipython
- /netcdf4-python
- /pygrib
- /pflexpart
- /netrc
- Plone -- Old notes for reference
Sub Pages
<subpages />