Difference between revisions of "Category:Python"
Basal@uio.no (talk | contribs) (Created page with "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 nu...") |
(Added tutorials) |
||
(11 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | 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. | + | 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. If you are not convinced yet, [https://wiki.uio.no/mn/geo/geoit/index.php/File:PickUpPython.pdf here] is an article you should read! |
+ | |||
+ | |||
+ | |||
+ | 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. | ||
+ | |||
+ | |||
== Key Topics / Tips for Python at UiO == | == Key Topics / Tips for Python at UiO == | ||
Line 28: | Line 34: | ||
*[https://store.continuum.io/cshop/academicanaconda <b>Anaconda</b>] | *[https://store.continuum.io/cshop/academicanaconda <b>Anaconda</b>] | ||
+ | |||
+ | Follow instructions given at https://www.continuum.io/anaconda-academic-subscriptions-available and make sure you use your university email address to register (otherwise you won't get your license for free!). | ||
Once you receive the license file just place it in the $HOME/.continuum folder. Don't forget "." in front of the folder name (this folder is hidden); if it does not exist, you can create it: | Once you receive the license file just place it in the $HOME/.continuum folder. Don't forget "." in front of the folder name (this folder is hidden); if it does not exist, you can create it: | ||
Line 48: | Line 56: | ||
− | At the command line. | + | At the command line. |
− | |||
− | |||
− | + | == <br/>Setting Paths == | |
− | + | 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 has been added to your <code>PYTHONPATH</code> . | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | 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 | ||
For more information see: | For more information see: | ||
− | *[ | + | *[https://wiki.uio.no/mn/geo/geoit/index.php/Python/SettingPaths /SettingPaths] |
− | |||
− | + | ==== Tutorials ==== | |
+ | See | ||
+ | * [https://annefou.github.io/metos_python/ Working with Spatio-temporal data in Python] | ||
== Best Practices == | == Best Practices == | ||
Line 108: | Line 109: | ||
You may find some examples, particularly when learning [http://matplotlib.sourceforge.net/ matplotlib] and working with 'pylab', where the tutorial uses: | You may find some examples, particularly when learning [http://matplotlib.sourceforge.net/ matplotlib] and working with 'pylab', where the tutorial uses: | ||
− | <div dir="ltr" class="mw-geshi mw-code mw-content-ltr"><div class="python source-python"><pre class="de1 | + | <div dir="ltr" class="mw-geshi mw-code mw-content-ltr"><div class="python source-python"><pre class="de1">from pylab import *</pre></div></div> |
− | + | <br/>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. | |
− | |||
− | 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: | The '''RECOMMENDED''' way to import modules is to: | ||
− | <div dir="ltr" class="mw-geshi mw-code mw-content-ltr"><div class="python source-python"><pre class="de1 | + | <div dir="ltr" class="mw-geshi mw-code mw-content-ltr"><div class="python source-python"><pre class="de1">import pylab as plb</pre></div></div> |
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. | 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. | ||
Line 120: | Line 119: | ||
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: | 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: | ||
− | <div dir="ltr" class="mw-geshi mw-code mw-content-ltr"><div class="python source-python"><pre class="de1 | + | <div dir="ltr" class="mw-geshi mw-code mw-content-ltr"><div class="python source-python"><pre class="de1">import matplotlib.pyplot as plt |
− | + | import numpy as np | |
| | ||
− | x | + | x = np.arange(100) |
− | y | + | y = np.sin(x) |
| | ||
− | fig | + | fig = plt.figure() |
− | ax | + | ax = fig.add_subplot(111) |
− | ax. | + | ax.plot(x, y) |
| | ||
− | plt. | + | plt.show()</pre></div></div> |
− | Here we have shown an example of both the import of matplotlib.pyplot into the 'plt.' namespace, and numpy into the 'np.' namespace | + | Here we have shown an example of both the import of matplotlib.pyplot into the 'plt.' namespace, and numpy into the 'np.' namespace. |
− | |||
− | |||
== Useful Software / Modules == | == Useful Software / Modules == | ||
− | *[ | + | *<div>[https://wiki.uio.no/mn/geo/geoit/index.php/Python/Sphinx Sphinx]<br/></div> |
− | *[ | + | *<div>[https://wiki.uio.no/mn/geo/geoit/index.php/Python/PlotMetData <span dir="auto">PlotMetData</span>]<br/></div> |
− | *[ | + | *<div>[https://wiki.uio.no/mn/geo/geoit/index.php/Python/Ipython Ipython]<br/></div> |
− | + | *<div>[https://wiki.uio.no/mn/geo/geoit/index.php/Python/pflexpart pflexpart]<br/></div> | |
− | + | *<div>[https://wiki.uio.no/mn/geo/geoit/index.php/Python/netrc netrc]<br/></div> | |
− | *[ | + | *<div>[[Plone]] -- Old notes for reference<br/><br/><br/><br/><br/></div><br/><br/><br/><br/> |
− | |||
− | |||
− | *[ | ||
− | *[[Plone]] -- Old notes for reference | ||
− | |||
− | |||
− | |||
− | |||
[[Category:Python]]<br/>[[Category:Software]]<br/>[[Category:Tools]] | [[Category:Python]]<br/>[[Category:Software]]<br/>[[Category:Tools]] |
Latest revision as of 16:43, 25 January 2018
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. If you are not convinced yet, here is an article you should read!
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
By Anne Fouilloux Published May 22, 2014 09:59 AM - Last modified May 22, 2014 10:17 AM
Python is an interpreted, interactive, object-oriented programming language. It incorporates modules, exceptions, dynamic typing, very high level dynamic data types, and classes. Python combines remarkable power with very clear syntax. It has interfaces to many system calls and libraries, as well as to various window systems, and is extensible in C or C++.
UiO Geosciences IT provides users with two different python distributions to work with:
Both are available on: Red Hat 6 linux servers and desktops, use e.g sverdrup.uio.no
How to start:
If you wish to use Enthought Python Distribution:
module load python/canopy
or for Anaconda Python Distribution:
Before using Anaconda Python Distribution, you would need to get your own academic license. Academic licenses are free and can be generated for/by each user from:
Follow instructions given at https://www.continuum.io/anaconda-academic-subscriptions-available and make sure you use your university email address to register (otherwise you won't get your license for free!).
Once you receive the license file just place it in the $HOME/.continuum folder. Don't forget "." in front of the folder name (this folder is hidden); if it does not exist, you can create it:
mkdir $HOME/.continuum
The email accompanying the license will have more detailed instructions. You may also reference the online anaconda documentation for further detail.
Then to set-up your environment:
module load python/anaconda
If you have problems with the module command please follow this link.
Then for any of these two distributions:
python
At the command line.
Setting Paths
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 has been added to your PYTHONPATH
.
For more information see:
Tutorials
See
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.
Useful Software / Modules
Pages in category "Python"
The following 2 pages are in this category, out of 2 total.