Transcript users/PBI

Drawing phylogenies
We've seen several tree data structures but we still
can't draw a tree 
In today's first exercise we write a drawtree
module for the Phylogeny class:
1
Drawing other trees
Can we reuse this phylogeny drawtree method with other trees?
• drawtree needs to know child nodes and name of node
No problem: A node in any tree has some sons (or none)
and a name.
Phylogeny-specifics for drawtree:
• uses method get_sons (returns list of child nodes)
• used method get_name (returns string)
Problem: Corresponding methods in other trees may have
different names
2
Can't use drawtree directly
We want:
General tree drawing tool that can draw any tree if given a
converter object: an "interpreter" which knows what methods to
call for the given kind of node instead of
get_sons
get_name
• For each tree data structure we need to write a converter
• Need to modify drawtree slightly to use a converter
3
A converter class for tree data structure X
• Should have two methods, each taking an X node as argument:
– one for returning a list of the node's sons
– one for returning the name of the node
4
New version of drawtree module
• All functions that take a node some_node as argument
should also take a converter object converter
• All occurrences of
some_node.get_name() and
some_node.get_sons()
are replaced with
converter.get_name( some_node ) and
converter.get_sons( some_node )
5
Example: converter for XML DOM trees
Recall: a DOM tree consists of nodes representing
tags in the XML data
- pretty simple, huh?
6
Drawing XML DOM trees
Import modified
drawtree module
Parse the XML file,
obtain DOM tree
Please draw this tree,
here is how (neat!)
7
Drawing
article2.xml
8
And now for something completely different
os module: Interface to operating system functionality
os.linesep
The string used to separate (or, rather, terminate) lines on the current
platform. This may be a single character, such as '\n' for Unix or
'\r' for Mac OS, or multiple characters, for example, '\r\n' for
Windows.
os.getcwd()
Returns a string representing the current directory
os.mkdir( path )
Creates a directory with the given name
os.listdir( path )
Returns a list of all entries in the given directory path
9
os.path module: functions on pathnames
os.path.split( path )
Split the pathname path into a pair, (head, tail) where tail is the part after the
last / and head is everything leading up to that.
os.path.basename( path ), os.path.dirname( path )
Returns the head and tail of path, respectively.
os.path.splitext( path )
Split the path into a pair (root, ext) such that root + ext == path, and ext is
the extension.
os.path.exists( path )
Returns true if the given file or directory exists, false otherwise.
os.path.join( path1[, path2[, ...]] )
Joins the paths to form a new path in valid format. E.g.
os.path.join( "/users/chili/PBI", "plan.html" )
10
os.walk - recursively
traverse directory
Programs_from_
book symbolic link
not included
os.walk( top )
walk() generates the file names in a directory tree, by walking the tree either
top down or bottom up.
For each directory in the tree rooted at directory top (including top itself), it
yields a 3-tuple (dirpath, dirnames, filenames).
dirpath is a string, the path to the directory. dirnames is a list of the names
of the subdirectories in dirpath (excluding '.' and '..'). filenames is a list of
11
the names of the non-directory files in dirpath.
Calculate size of all files in PBI directories
dirwalk.py
No path info in dirs
and files lists; join
with root
Traversal is top-down by default:
OK to remove a directory from the
dirs list to avoid visiting it
12
Note: Project not visited
/users/chili/PBI/ consumes 155940 bytes in 45 non-directory files
/users/chili/PBI/Exercises consumes 1289251 bytes in 148 nondirectory files
/users/chili/PBI/Exercises/Solutions consumes 1095594 bytes in 109
non-directory files
/users/chili/PBI/Slides consumes 4974264 bytes in 22 non-directory
files
/users/chili/PBI/Slides/Images consumes 1173961 bytes in 70 nondirectory files
/users/chili/PBI/Mail consumes 6625 bytes in 7 non-directory files
/users/chili/PBI/NoAccess consumes 0 bytes in 0 non-directory files
/users/chili/PBI/ExamplePrograms consumes 770219 bytes in 160 nondirectory files
13
dirdraw.py
Now that we have a drawtree function..
14
Tadaa!
+---Solutions
+---------Exercises+
|
+-----Project
|
+------------Slides+------Images
---PBI+
+--------------Mail
|
+----------NoAccess
|
+---ExamplePrograms
15