Transcript Node

Drawing phylogenies
We've seen several tree data structures but we still
can't draw a tree 
In the tree drawing exercise we write a drawtree
function for the Node 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.
Node 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
node
4
New version of drawtree module
• All functions that call get_name or get_sons on a node
some_node must also take a converter object
converter as argument
• 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
xml_draw.py (part 1)
A DOM tree consists of nodes representing tags in the XML data
drawtree doesn’t know how to get to the sons and the
name of a DOM tree node, but this XML converter tells it how
6
Drawing XML DOM trees
xml_draw.py (part 2)
Import modified
drawtree module
Parse 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 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.dirname( path ) , os.path.basename( 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 format valid on current platform:
os.path.join( "/users/chili/PBI", "plan.html" ) gives
/users/chili/PBI/plan.html
10
os.walk - recursively
traverse directory
Notes
figures
ProgramExamples
FromCourseNotes
os.walk( top )
walk() generates the entries in a directory tree, by walking the tree either top
down or bottom up. It returns a list of tuples:
Each directory in the tree rooted at directory top (including top itself) 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.
dirwalk.py
Find all python programs in PBI directories
Traversal is top-down by default:
OK to remove a directory from the
subdirs list to avoid visiting it
12
Notes
figures
ProgramExamples
FromCourseNotes
/users/chili/PBI/Exercises contains 3 .py files (out of 211 files)
/users/chili/PBI/Exercises/Solutions contains 29 .py files (out of 67 files)
/users/chili/PBI/ProgramExamples contains 48 .py files (out of 89 files)
/users/chili/PBI/ProgramExamples/FromCourseNotes contains 25 .py files (out
of 27 files)
13
Now that we have a drawtree function..
dirdraw.py
entry names only local: join
with root to get full path
14
Tadaa!
15
..on to the exercises
16