Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Functions and modules
Karin Lagesen
karin.lagesen@bio.uio.no
Modularization
●
Programs can get big
●
Risk of doing the same thing many times
●
Functions and modules encourage
●
re-usability
●
readability
●
helps with maintenance
Functions
●
●
●
●
Most common way to modularize a
program
Takes values as parameters, executes
code on them, returns results
Functions also found builtin to Python:
●
open(filename, mode)
●
str(number)
These do something on their parameters,
and returns the results
Functions – how to define
●
How to define a function:
def FunctionName(param1, param2, ...):
FUNCTION CODE …
return DATA
Note the :
Part of the
definition!
●
keyword: def – says this is a function
●
functions need names - FunctionName
●
parameters are optional, but common
●
FUNCTION CODE does something
●
keyword return results: return
●
return is optional
Function example
>>> def hello(name):
... results = "Hello World to " + name + "!"
... return results
... >>> hello()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: hello() takes exactly 1 argument (0 given)
>>> hello("Marie")
'Hello World to Marie!'
>>> ●
●
Task: make script from this – take name
from command line
Print results to screen
function.py
[karinlag@freebee]~/teaching% cat function.py import sys
def hello(name):
# This function takes in a name and returns
# a nice greeting
results = "Hello World to " + name + "!"
return results
name = sys.argv[1]
functionresult = hello(name)
print functionresult
[karinlag@freebee]~/teaching% Function
definition
functionresult:
Variable that stores
the returned results
[karinlag@freebee]~/teaching% python function.py Traceback (most recent call last):
File "function.py", line 7, in <module>
name = sys.argv[1]
IndexError: list index out of range
[karinlag@freebee]~/teaching% python function.py Marie
Hello World to Marie!
[karinlag@freebee]~/teaching% Function scope
●
●
Variables defined inside a function can
only be seen there!
Access the value of variables defined
inside of function: return variable
Scope example
>>> def test(x):
... z = 10
... print "the value of z is " + str(z)
... return x*2
... >>> z = 50
>>> test(3)
the value of z is 10
6
>>> z
50
>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>> Parameters
●
●
Functions can take parameters – not
mandatory
Parameters are positional: follow the order
in which they are given
>>> def test(x, y):
... print x*2
... print y + str(x)
... >>> test(2, "y")
4
y2
>>> test("y", 2)
yy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in test
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> parse_blast_function.py
●
Modification 4:
–
Create function that looks like this:
def format_blast(blastline):
create output string containing isotig,
contig, identity, e-value, finish with newline
Return output string
–
For loop that, per line in input,
If identity is above 99.5:
calls format_blast with line as parameter
writes output to file
–
Close output file
parse_blast_function.py
[karinlag@freebee]~/teaching% cat parse_blast_function.py
import sys
def format_blast(blastline):
# Function that parses the blast line so that the # subject, query, identity and evalue is returned
withoutnewline = line[:1]
fields = withoutnewline.split()
outstring = fields[0] + "\t" + fields[1] + "\t" + \
fields[2] + "\t" + fields[10] + "\t" + "\n"
return outstring
# Input file is first
filename = sys.argv[1]
# Then I specify output file
outname = sys.argv[2]
Function
definition
parse_blast_function.py cont.
# Open and read in file contents
fh = open(filename, "r")
lines = fh.readlines()
fh.close()
fo = open(outname, "w")
for line in lines:
# Split line on whitespace
fields = line.split()
# Convert to float so that I can compare
identity = float(fields[2])
# Here I do the comparison
if identity >= 99.5:
# I call the function, and a nicely
# formatted line is returned
outstring = format_blast(line)
fo.write(outstring)
fo.close()
[karinlag@freebee]~/teaching% outstring:
Variable that stores
the returned results
Modules
●
A module is a file with functions, constants
and other code in it
●
Module name = filename without .py
●
Can be used inside another program
●
Needs to be import-ed into program
●
Lots of builtin modules: sys, os, os.path....
●
Can also create your own
Using module
●
One of two import statements:
1: import modulename
2: from module import function/constant
●
If method 1:
●
●
modulename.function(arguments)
If method 2:
●
●
function(arguments) – module name not
needed
beware of function name collision
Operating system modules –
os and os.path
●
●
Modules dealing with files and operating
system interaction
Commonly used methods:
●
os.getcwd() - get working directory
●
os.chdir(path) – change working directory
●
os.listdir([dir = .]) - get a list of all
files in this directory
●
os.mkdir(path) – create directory