Download presentation
Presentation is loading. Please wait.
1
1 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:
2
2 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
3
3 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
4
4 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
5
5 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 )
6
6 Example: converter for XML DOM trees 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 xml_draw.py (part 1)
7
7 Drawing XML DOM trees Import modified drawtree module Parse XML file, obtain DOM tree Please draw this tree, here is how (neat!) xml_draw.py (part 2)
8
8 Drawing article2.xml
9
9 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
10
10 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
11
11 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 the names of the non-directory files in dirpath. os.walk - recursively traverse directory NotesProgramExamples figuresFromCourseNotes
12
12 Find all python programs in PBI directories dirwalk.py Traversal is top-down by default: OK to remove a directory from the subdirs list to avoid visiting it
13
13 /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) NotesProgramExamples figuresFromCourseNotes
14
14 Now that we have a drawtree function.. dirdraw.py entry names only local: join with root to get full path
15
15 Tadaa!
16
16..on to the exercises
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.