Useful Python modules: Difference between revisions

From Training Material
Jump to navigation Jump to search
 
No edit summary
 
Line 5: Line 5:
The time module offers functions for getting the current time and date.
The time module offers functions for getting the current time and date.


<source lang="python">
syntaxhighlight lang="python">
import time
import time
s = time.asctime() # as string
s = time.asctime() # as string
i = time.time() # as float
i = time.time() # as float
</source>
</syntaxhighlight>


== Accessing web pages ==
== Accessing web pages ==
The HTML code of web pages and downloadable files from the web can be accessed in a similar way as reading files:
The HTML code of web pages and downloadable files from the web can be accessed in a similar way as reading files:


<source lang="python">
<syntaxhighlight lang="python">
import urllib
import urllib
url = 'http://www.google.com'
url = 'http://www.google.com'
page = urllib.urlopen(url)
page = urllib.urlopen(url)
print page.read()
print page.read()
</source>
</syntaxhighlight>


== Finding out what is in a module ==
== Finding out what is in a module ==
The contents of any module can be examined with:
The contents of any module can be examined with:


<source lang="python">
<syntaxhighlightlang="python">
print dir( name_of_module )
print dir( name_of_module )
help( name_of_module )
help( name_of_module )
</source>
</syntaxhighlight>


== Creating plots ==
== Creating plots ==
The Matplotlib library contains a wide range of possibilities for creating graphs. See the 'gallery' link on http://matplotlib.sourceforge.net for examples. It needs to be installed separately.
The Matplotlib library contains a wide range of possibilities for creating graphs. See the 'gallery' link on http://matplotlib.sourceforge.net for examples. It needs to be installed separately.


<source lang="python">
<syntaxhighlight lang="python">
from pylab import *
from pylab import *
</source>
</syntaxhighlight>


== Database access ==
== Database access ==
Numpy is a library that allows operating on arrays and matrices. It needs to be installed separately. See: http://numpy.scipy.org
Numpy is a library that allows operating on arrays and matrices. It needs to be installed separately. See: http://numpy.scipy.org


<source lang="python">
<syntaxhighlight lang="python">
import numpy
import numpy
a = numpy.array( [1,2,3,4,5,6] )
a = numpy.array( [1,2,3,4,5,6] )
print a+10, a*a
print a+10, a*a
</source>
</syntaxhighlight>


== Image manipulation ==
== Image manipulation ==
The Python Imaging Library PIL is a powerful tool for working with images (changing formats, resizing, cutting, drawing). It needs to be installed separately. See http://www.pythonware.com/products/pil .
The Python Imaging Library PIL is a powerful tool for working with images (changing formats, resizing, cutting, drawing). It needs to be installed separately. See http://www.pythonware.com/products/pil .

Latest revision as of 07:56, 2 June 2025


Getting the current time and date

The time module offers functions for getting the current time and date.

syntaxhighlight lang="python"> import time s = time.asctime() # as string i = time.time() # as float </syntaxhighlight>

Accessing web pages

The HTML code of web pages and downloadable files from the web can be accessed in a similar way as reading files:

import urllib
url = 'http://www.google.com'
page = urllib.urlopen(url)
print page.read()

Finding out what is in a module

The contents of any module can be examined with:

<syntaxhighlightlang="python"> print dir( name_of_module ) help( name_of_module ) </syntaxhighlight>

Creating plots

The Matplotlib library contains a wide range of possibilities for creating graphs. See the 'gallery' link on http://matplotlib.sourceforge.net for examples. It needs to be installed separately.

from pylab import *

Database access

Numpy is a library that allows operating on arrays and matrices. It needs to be installed separately. See: http://numpy.scipy.org

import numpy
a = numpy.array( [1,2,3,4,5,6] )
print a+10, a*a

Image manipulation

The Python Imaging Library PIL is a powerful tool for working with images (changing formats, resizing, cutting, drawing). It needs to be installed separately. See http://www.pythonware.com/products/pil .