150 likes | 251 Views
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:. Drawing other trees. Can we reuse this phylogeny drawtree method with other trees?
E N D
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:
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
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
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
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 )
Example: converter for XML DOM trees Recall: a DOM tree consists of nodes representing tags in the XML data - pretty simple, huh?
Drawing XML DOM trees Import modified drawtree module Parse the XML file, obtain DOM tree Please draw this tree, here is how (neat!)
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
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" )
os.walk - recursively traverse directory 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 the names of the non-directory files in dirpath. Programs_from_booksymbolic link not included
Calculate size of all files in PBI directories No path info in dirs and files lists; join with root dirwalk.py Traversal is top-down by default: OK to remove a directory from the dirs list to avoid visiting it
Note: Project not visited /users/chili/PBI/ consumes 155940 bytes in 45 non-directory files /users/chili/PBI/Exercises consumes 1289251 bytes in 148 non-directory 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 non-directory 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 non-directory files
Now that we have a drawtree function.. dirdraw.py
Tadaa! +---Solutions +---------Exercises+ | +-----Project | +------------Slides+------Images ---PBI+ +--------------Mail | +----------NoAccess | +---ExamplePrograms