Miscellaneous modules
Below we list some of the standard Python modules in terms of their general function and
give a few simple examples. Full documentation for these modules can be found at the
official
Python
website
at
http://docs.python.org/2/library/
or
http://docs.python.org/3/library/
. These are modules that will need to be imported into a
Python script, but which should be present as standard in the Python installation. It should
be noted that the re module is described in extensive detail later in
Appendix 5
.
Module
Description
Example
argparse
A module that
helps interpret
command line
options/arguments,
i.e. information
typed after the
name of a
program, as
available in
sys.argv.
from argparse import ArgumentParser
parser = ArgumentParser(prog='MyProgram')
parser.add_argument('-x', type=float, help='Help
for X')
parser.add_argument('-y', type=int, nargs='?',
>default=1)
parser.print_help()
array
A packed numeric
array object, i.e.
an ordered
collection
containing all
numbers or
characters, with a
given data type
(specified with a
one-letter code).
Not as capable as
numpy.array but
part of standard
Python and
efficient for
interpreting
‘binary’ data.
from array import array
data = [9,7,5,4]
x = array('i', data) # Int type
len(x) # 4
copy
Creates a new
Python object by
copying an
existing object.
Can create shallow
or deep copies,
where any object
contained by an
object is itself also
copied.
from copy import copy, deepcopy
x = [[1,2], [3,4]]
y = copy(x) # Same _contents_
z = deepcopy(x) # All new
x[1].append(5)
print(y) # [[1, 2], [3, 4, 5]]
print(z) # [[1, 2], [3, 4]]
cStringIO
(Python 2)
or
io
Used to create an
object that can be
used to read string
data as if it were a
Python 2:
from cStringIO import StringIO
obj = StringIO('Start\nMid\nEnd\n')
obj.readline() # 'Start\n'
(Python 3)
file.
Python 3:
from io import StringIO
datetime
A module that
contains date,
time, timedelta
and datetime
objects to
represent temporal
information. Deals
with daylight
savings, date
formatting, time
string
interpretation etc.
from datetime import datetime
text = '07/May/1945 02:41'
format = '%d/%b/%Y %H:%M'
dt = datetime.strptime(text,format)
dt.month # 5
dt.ctime()
# 'Mon May 7 02:41:00 1945'
fnmatch
Provides file name
matching using
UNIX-like wild
cards, i.e. patterns
that include ‘*’
and ‘?’ rather than
regular
expressions.
from os import listdir
from fnmatch import fnmatch
for file in listdir('.'):
if fnmatch(file,'*.txt'):
print(file)
ftplib
Used to send and
receive files using
the File Transfer
Protocol.
from ftplib import FTP
ftpSession = FTP('ftp.ncbi.gov',
username,password)
ftpSession.cwd('genomes')
ftpSession.dir()
ftpSession.retrbinary('RETR: remoteFile')
ftpSession.storLines('STOR localfile')
ftpSession.quit()
gzip, bz2,
zipfile, tarfile
Libraries that deal
with creating and
extracting
compressed and/or
archived files.
import gzip
fileObj = gzip.open('data.gz')
for line in fileObj:
print(line)
httplib
(Python 2)
Used to send and
receive
Python 2:
from httplib import HTTPConnection
or
http.client
(Python 3)
information across
the Internet using
the Hypertext
Transport
Protocol. A lower-
level library than
urllib/urllib2.
conObj = HTTPConnection("www.python.org")
conObj.request("GET","/index.html")
resp = conObj.getresponse()
print(resp.status)
print(resp.read())
Python 3:
from http.client
import HTTPConnection
multiprocessing
Runs Python code
as separate,
parallel, processes.
Generally used on
multiple
core/processor
systems.
from multiprocessing import Process
job1 = Process(target=calcFunc,
args=work1)
job2 = Process(target=calcFunc,
args=work2)
job1.start()
job2.start()
job1.join()
job2.join()
platform
Used to get
information about
the current
computer and its
architecture.
import platform
platform.processor()
# e.g. 'x86_64'
platform.python_version()
# e.g. '2.7.3'
platform.architecture()
# e.g. ('64bit', 'ELF')
platform.node()
# e.g. 'MyPC'
re
Regular
expressions; used
to find and replace
substrings using
pattern matching.
See
Appendix 5
.
See
Appendix 5
.
sqlite3
Allows interaction
with a lightweight
SQL database
called SQLite.
import sqlite3
conn = sqlite3.connect('myDb')
cursor = conn.cursor()
stmt = "select * from structure where
pdbId='1AFO'"
cursor.execute(stmt)
result = cursor.fetchall()
cursor.close()
conn.close()
subprocess
Runs an external
program as a
separate
job/process and
connects any
input/output data
streams.
from subprocess import call
command = 'clustalw seq.fasta'
call(command, stdIn=filObj)
threading
Runs Python code
in separate
threads. These will
not run
concurrently on
multiprocessor
systems (use
multiprocessing
instead for that),
but can be useful
to process
intermittent data
streams.
from threading import Thread
job1 = Thread(target=calcFunc,
args=work1)
job2 = Thread(target=calcFunc,
args=work2)
job1.start()
job2.start()
job1.join()
job2.join()
urllib, urllib2
(Python 2)
or
urllib.request,
urllib.parse,
urllib.error
(Python 3)
Used to send and
receive
information across
the Internet: a
higher-level, and
so often more
convenient, library
than httplib.
Handles web
proxies,
redirection,
passwords,
cookies etc. Often
used to interact
with web services
and databases.
Python 2:
import urllib, urllib2
optionDict = {'format':'PDB',
'compression':'None'}
optionStr = urllib.urlencode(optionDict)
url =
'http://www.rcsb.org/pdb/cgi/export.cgi/1OUN.pdb'
req = urllib2.Request(url, optionStr)
resp = urllib2.urlopen(req)
print(resp.read())
Python 3:
import urllib, urllib.request
optionDict = {'format':'PDB',
'compression':'None'}
optionStr =
urllib.parse.urlencode(optionDict).encode('utf-
8')
url =
'http://www.rcsb.org/pdb/cgi/export.cgi/1OUN.pdb'
req = urllib.request.Request(url, optionStr)
resp = urllib.request.urlopen(req)
print(resp.read().decode('utf-8'))
zlib
Used to compress
data into more
compact
representation,
using the zlib
algorithm. Can be
useful for caches
and undo
functions.
import zlib
x = zlib.compress('Bananarama')
print(x, zlib.decompress(x))
Numerical Python: ‘
numpy
’
The table below details a subset of the functionality available from the numpy module. For
brevity we have only included some of the more commonly used aspects as well as those
mentioned in this book. Often we have not included all the possible arguments for a
function, instead only focussing on what we deem most important. Fuller documentation
may be found at the NumPy and SciPy website:
http://docs.scipy.org/doc/
. It should be
noted that the numpy module is not part of the standard Python library, and as such is
installed separately.
Although not listed in the table, the numpy module includes many mathematical
operations, e.g. abs, sqrt, exp, log, power, cos, sin, tan, arccos, arcsin, arctan cosh, sinh
and tanh, which sometimes share the same names as standard math methods. These
methods accept single values (so may be used instead of math methods) and also operate
on arrays, where they act in an element-wise manner.
NumPy has more numeric data types than standard Python, which may be used to
represent numbers with various numbers of bits (and thus also levels of precision).
Examples include: int, int8, int16, uint8, uint16, float, float32 etc. The types int and float,
without a bit specification, will correspond to the same data type as regular Python
(although this is specific to the system, i.e. 32 or 64 bit).
In NumPy the most important class is the container object array and many of the
functions which may be independently imported from numpy are also bound methods of
array. Thus, for example, to calculate a dot product we could do direct imports:
from numpy import array, dot
a1 = array([1, 2, 3])
a2 = array([4, 5, 6])
dp = dot(a1, a2)
Or use a bound method:
from numpy import array
a1 = array([1, 2, 3])
a2 = array([4, 5, 6])
dp = a1.dot(a2)
We list both approaches in the table, which naturally assumes the appropriate modules
are imported for each case. It should also be noted that the input arguments generally do
not themselves need to be NumPy array (or matrix) objects. In many cases data can be
input in the form of an ‘array-like’ sequence of values, which would typically include
tuples and lists of numbers.
Do'stlaringiz bilan baham: |