[Tutor] problem calling a function

Vincent Wan wan at walrus.us
Sun Nov 13 06:55:31 CET 2005


Dear all,

I have a simple program that calls a function I got off of active  
state that draws
a tree given a description of it in standard string form.

My code generates a string description and calls the function to draw  
the tree.
Instead of drawing the tree it echos the string. But when I call the  
function
interactively with the same string it prints the tree! Why dosn't it  
work non-interactivly

here is my IDLE run

(((('0','9'),('4','6')),'2'),(('1',(('5','8'),'7')),'3'))
(((('0','9'),('4','6')),'2'),(('1',(('5','8'),'7')),'3')) --

 >>> printDendrogram((((('0','9'),('4','6')),'2'),(('1', 
(('5','8'),'7')),'3')))
0 -----+
        |--+
9 -----+  |
           |--+
4 -----+  |  |
        |--+  |
6 -----+     |
              |--+
2 -----------+  |
                 |--
1 --------+     |
           |--+  |
5 --+     |  |  |
     |--+  |  |  |
8 --+  |  |  |  |
        |--+  |  |
7 -----+     |  |
              |--+
3 -----------+

here is my code

# Thesis_ex_gen6.py

import random

# constants that control the simulation
MAX_LINAGES = 10
BRANCHING_PROBABILITY = 0.01
EXTINCTION_PROBABILITY = 0.01

def printDendrogram(T, sep=3):

     """Print dendrogram of a binary tree.  Each tree node is  
represented by a length-2 tuple.
        routine written by David Eppstein from ActiveState Programers  
Network Last Updated: 2002/07/13"""
	
     def isPair(T):
         return type(T) == tuple and len(T) == 2

     def maxHeight(T):
         if isPair(T):
             h = max(maxHeight(T[0]), maxHeight(T[1]))
         else:
             h = len(str(T))
         return h + sep

     activeLevels = {}

     def traverse(T, h, isFirst):
         if isPair(T):
             traverse(T[0], h-sep, 1)
             s = [' ']*(h-sep)
             s.append('|')
         else:
             s = list(str(T))
             s.append(' ')

         while len(s) < h:
             s.append('-')

         if (isFirst >= 0):
             s.append('+')
             if isFirst:
                 activeLevels[h] = 1
             else:
                 del activeLevels[h]

         A = list(activeLevels)
         A.sort()
         for L in A:
             if len(s) < L:
                 while len(s) < L:
                     s.append(' ')
                 s.append('|')

         print ''.join(s)

         if isPair(T):
             traverse(T[1], h-sep, 0)

     traverse(T, maxHeight(T), -1)


for x in range(1):

     print '\n','run ',  x+1, '\n'


     next_linage = 0    # next counter initalized
     linages = [0]    # linages initalized
     num_linages = 1    # total linage counter initalized
     time = 0     # time initalized
     tree = "'0'" # tree initalized


     while (len(linages) != 0) and (num_linages < MAX_LINAGES):
         time += 1

         "With BRANCHING_PROBABILITY creates a new linage and prints  
information"
         i = 0
         while i < len(linages):
             if random.random() < BRANCHING_PROBABILITY:
                 next_linage += 1
                 linages.append(next_linage)
                 print 'At ', time,' linage ', linages[i], 'evolved  
', next_linage

                 parent =  "'" + str(linages[i]) + "'"
                 parent_and_child = '(' + parent + ",'" + str 
(next_linage) + "')"
                 tree = tree.replace(parent, parent_and_child)
                 num_linages += 1
                 if num_linages == 10: break
             i += 1

         "With EXTINCTION_PROBABILITY kill a linage and print  
information"
         j = 0
         while j < len(linages):
             if random.random() < EXTINCTION_PROBABILITY:
                 print 'At ', time,' linage ', linages[j], 'died '
                 del linages[j]
                 if len(linages) == 0:
                     "restarts if fewer that 10 linages evolved"
                     print '\n restart \n'
                     next_linage = 0    # next counter initalized
                     linages = [0]    # linages initalized
                     num_linages = 1    # total linage counter  
initalized
                     time = 0     # time initalized
                     tree = "'0'"
             j += 1


print tree
printDendrogram(tree)

Thank you,

Vincent Wan

------------------------------------------------------------------------ 
--------------
PhD Candidate
Committee on the Conceptual and Historical Studies of Science
University of Chicago

PO Box 73727
Fairbanks, AK 99707

wan AT walrus DOT us (change CAPS to @ and . )



More information about the Tutor mailing list