Yesterday I got distracted again for a bit (Oct 14). I was playing with a thing called freemind (partially as my wife wants a package that can do work-breakdown-structures). FreeMind It occured to me that the FreeMind display lookes very like a phylogenetic tree. And the freemind file format is xml - and very simple xml too - simple enough that I can understand & generate it easily.
I hacked a dot file I had generated into freemind's xml form and it loaded nicely. It could be quite useful I think. You can browse the tree quickly, ... also hide or open branches, add notes/colours/whatever to nodes, export the tree to html.
Hacking dot files is hard work if you have to do it every time so I took a look into PI's code tree.py and I got sucked in and suddently I was attempting to program in python! eek. I succeded in generating freemind xml.
What's this all about huh?
Here.
A screenshot: (thanks to ImageMagick)
Here are generated files:
Here be diff and source: (this main.py and The Protocol Informatics Project are Copyright © 2004 Marshall Beddoe)
FreeMind comes precompiled for Windows/MacOSX/linux though I used the source package to install. It had no other dependancies. My machine here is getting a bit old and slow but freemind runs nice & zippily.
# warning: I don't know what I'm doing when it comes to python # www.python.org/moin/BeginnersGuide_2fProgrammers # diveintopython.org/toc/index.html # www.zvon.org/other/python/doc21/lib/python.html # docs.python.org/lib/lib.html # rgruet.free.fr/PQR2.3.html ### I needed to look at data structures I didn't understand # www.informatik.hu-berlin.de/Themen/manuals/python/python-texinfo/marshal.html # www.sourcekeg.co.uk/www.python.org/doc/2.2.3/lib/module-marshal.html import marshal,sys #print marshal.dump( (1, 2.0, 'name', [2,3,5,7]), sys.stdout ) print "\ndumps", marshal.dumps( (1, 2.0, 'name', [2,3,5,7]) ) #[jamesc@betty] ~/src/PI/data/$ python pyxml.py >pyxml.out #[jamesc@betty] ~/src/PI/data/$ xxd pyxml. #xxd: pyxml.: No such file or directory #[jamesc@betty] ~/src/PI/data/$ xxd pyxml.out #0000000: 0a64 756d 7073 2028 0400 0000 6901 0000 .dumps (....i... #0000010: 0066 0332 2e30 7304 0000 006e 616d 655b .f.2.0s....name[ #0000020: 0400 0000 6902 0000 0069 0300 0000 6905 ....i....i....i. #0000030: 0000 0069 0700 0000 0a0a 786d 6c20 6475 ...i......xml du #0000040: 6d70 203c 3f78 6d6c 2076 6572 7369 6f6e mp <?xml version #0000050: 3d22 312e 3022 3f3e 3c6d 6172 7368 616c ="1.0"?><marshal #0000060: 3e3c 7475 706c 653e 3c69 6e74 3e31 3c2f ><tuple><int>1</ #0000070: 696e 743e 3c66 6c6f 6174 3e32 2e30 3c2f int><float>2.0</ #0000080: 666c 6f61 743e 3c73 7472 696e 673e 6e61 float><string>na #0000090: 6d65 3c2f 7374 7269 6e67 3e3c 6c69 7374 me</string><list #00000a0: 2069 643d 2269 3222 3e3c 696e 743e 323c id="i2"><int>2< #00000b0: 2f69 6e74 3e3c 696e 743e 333c 2f69 6e74 /int><int>3</int #00000c0: 3e3c 696e 743e 353c 2f69 6e74 3e3c 696e ><int>5</int><in #00000d0: 743e 373c 2f69 6e74 3e3c 2f6c 6973 743e t>7</int></list> #00000e0: 3c2f 7475 706c 653e 3c2f 6d61 7273 6861 </tuple></marsha #00000f0: 6c3e 0a l>. # www.amk.ca/python/simple/ns-filter.html ### using xml.marshal I could just print the dumped xml to screen and look at it # pyxml.sourceforge.net/ # prdownloads.sourceforge.net/pyxml/PyXML-0.8.3.tar.gz?download # pyxml.sourceforge.net/topics/howto/node26.html import xml.marshal.generic; # The interface is the same as the standard Python marshal module: # dump(value, file) and dumps(value) # convert value into XML and either write it to the given file or return # it as a string, while load(file) and loads(string) perform the reverse conversion. For example: print "\nxml dump", xml.marshal.generic.dumps( (1, 2.0, 'name', [2,3,5,7]) ) # okay. xml is lovely but we're better off writing data <map version .... <node .... by hand (print) # but the dumper is handy #import xml.marshal.generic; #print "\nxml dump", xml.marshal.generic.dumps( (1, 2.0, 'name', [2,3,5,7]) ) ### how to open and write files in python ### also formatted output logfile = open('test.log', 'w') logfile.write('test succeeded') indent = "iiiiiiiiiiiiii" v1 = 1234 l = "56.04%" #logfile.write( indent + '<node TEXT="', v1, '-' + l + '">' ) #logfile.write( indent + '<node TEXT="', v1, '-' + l + '">' ) s = "<node TEXT=\"%(node)d-%(percent)s\">\n" % { 'node':v1, 'percent':l} logfile.write( indent + s ) logfile.close() print file('test.log').read() level = 0 #level++ doesn't work #++level doesn't work level=level+1 level+=1 # don't know how to work the formatted print directive * in python print "\n"+'%*s', level*3, " "; # indent nodes by level in tree print "</node>\n"; print '%s has %03d quote types.' % ('Python', 2) == 'Python has 002 quote types.' a = '%(lang)s has %(c)03d quote types.' % {'c':2, 'lang':'Python'} print a level = 0 print "\n" print "mmm" * level print "</node>\n"; level += 1 print "\n" print "mmm" * level print "</node>\n"; level += 1 print "\n" print "mmm" * level print "</node>\n"; # www.awprofessional.com/articles/article.asp?p=28790&seqNum=2 ### for loop print '\nlevel is', level for n in range(2, level): print ' n is', n print "\n" ### string repeat using * indent = "\n" + " " * level # indent nodes by level in tree print indent, level, "\n" ### if not if not 0: print "! 0" if not 1: print "! 1"
home |