Programming in the large
1) Functions (Subroutines)
#!/usr/bin/env python
######### modules and global variables ############################
import re
closing_tag = re.compile(r"</.*?>")
######### functions ###############################################
def find_closing_tags(line):
result = closing_tag.search(line)
if result:
print result.group()
######### main program ############################################
file = open("example.html","r")
text = file.readlines()
file.close()
for line in text:
find_closing_tags(line)
Exercise
1.1 Add a second function to the example that prints all tags that
are not closing tags. Before the functions are called, let the main
program print a heading "these are the opening tags" and
"these are the closing tags". (Note: this script only finds the
first tag in every line. To change that so that it finds all tags
you would have to insert line breaks after the tags as shown
in exercise 4.2 from week 8.)
2) Passing arguments to and from a function
#!/usr/bin/env python
import os
######### functions #############################################
def read_file(filename):
file = open(filename, "r")
text = file.readlines()
file.close()
return text
def write_file(filename,text):
file = open(filename, "w")
file.writelines(text)
file.close()
######### main program ############################################
filecontent = read_file("example.html")
filecontent.append("this is the last line\n")
write_file("example.html", filecontent)
os.system("more example.html")
The main program sends the file name as argument
to the function read_file and receives
filecontent from it as return value.
The arguments in the function call and function declaration must
appear in the same order.
Exercises
2.1 Write a third function for the script. The function is called
get_input. It takes filecontent as argument and also returns it.
The function asks a user to input some text. The text is inserted
at the end of the file. Optional: the user is also asked for the
line where the text will be inserted.
2.2 Modify the previous example so that the user is asked for his/her
first name, last name, email address. The information is stored
in the file in the following format:
Mary | Smith | mary@somecompany.com
John | Miller | miller@somecompany.com
2.3 A fourth function is added that asks a user for her/his last name and
retrieves the email address from the file. To do that every line of the
file is
split into a list. The second element in the list is compared to
the current user's last name.
Optional material: Keyword arguments in the function call
If the argument names from the function declarations are used
in the function call, the arguments can be provided in any order.
For example, a function
def write_file(filename,text):
can be called with
write_file(text =filecontent, filename= "example.html")
Optional material: Default arguments in the function declaration
Default arguments can be provided in a function declaration.
But these must be listed after non-default arguments. For example,
the write_file function can be declared as
def write_file(text,filename = "defaultfile.html"):
and be called with
write_file(text =filecontent)
Optional material: Local and Global Variables
Each function should use its own, local set of variables so that
the different functions do not interfere with each other.
"Global variables" are variables that are used in the main part.
These are available in all functions. In the script under 1),
a global variable was used: closing_tag. The use of global variables
should be avoided.