[Tutor] please help me improve my python style

Vincent Wan wan at walrus.us
Fri Jan 14 01:58:42 CET 2005


I have my first python program working (thank you for the help all).  
Will anyone give me hints on how my program could be improved and on  
how I could improve my programing style?

I want it to be well written as it can be but also as close to self  
documenting and understandable to the average non-programer biologist.

Here is the first draft of my code:

# This program simulates the random branching and extinction of linages.
# It also mutates a series of characters representing morphology at  
each branch point
# The program replicates the program first described by D.M. Raup and  
S.G. Gould
# 1974 Systematic Zoology 23: 305-322.
# written by Vincent Wan with help from tutor at python.org

import random

debug = 0    # turns debug diagnostic printing on = 1 or off = 0

#constants that control the simulation
max_linages = 201    # the number of linage to be evolved + 1
branching_probablity = .1
extinction_probablity = .1
mutation_rate = .5

# initalize
living_linages = [[0, 0, 0, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]] # the  
ancestor
dead_linages = []
tree = "*0*"
time = 0
next_linage = 0

def Mutate_morphology(a_morphology, rate):
     "Each morphological character has a rate chance it is increased or  
decreased by 1"
     a_morphology = a_morphology[:]
     character = 0
     while character < len(a_morphology):
         if random.random() <= rate:
             if random.random() < .5:
                  a_morphology[character] = a_morphology[character] - 1
             else:a_morphology[character] = a_morphology[character] + 1
         character += 1
     return a_morphology

def Braching(debug, next_linage, time, living_linages, mutation_rate,  
tree):
     "With branching_probablity creates a new linage, mutates its  
morphology, and updates tree."
     counter = 0
     while counter < len(living_linages):
         if random.random() < branching_probablity:
             # creates new linage
             next_linage += 1
             new_linage = [next_linage, time, 0,  
Mutate_morphology(living_linages[counter][3], mutation_rate)]
             living_linages.append(new_linage)
             # updates linage tree
             target = '*' + str(living_linages[counter][0]) + '*'
             replacement = '(' + target + ',*' + str(new_linage[0]) +  
'*)'
             tree = tree.replace(target, replacement)
         counter += 1
         if debug: print 'at time ', time, ' living_linages: ',  
[linage[0] for linage in living_linages]
     return (next_linage, living_linages, tree)

def Extinction(debug, extinction_probablity, living_linages, time,  
dead_linages):
     "With extinction_probablity kills living species and adds them to  
the dead list"
     counter = 0
     while counter < len(living_linages):
         if random.random() < extinction_probablity:
             newly_dead = living_linages[counter]
             newly_dead[2] = time
             dead_linages.append(newly_dead)
             living_linages.remove(living_linages[counter])
             if len(living_linages) == 0: break    # when the last  
living_linages goes extinct exit
         counter += 1
     if debug: print 'at time ', time, ' dead_linages : ', [linage[0]  
for linage in dead_linages]
     return (living_linages, dead_linages)

def Print_results(next_linage, max_linages, living_linages,  
dead_linages, tree):
     "prints number of linages, the data about the linages living and  
dead, and the tree"
     if next_linage < max_linages:
         print '\nall extinct with only ', next_linage + 1, ' linage of  
', max_linages
     else:
         print '\n', max_linages - 1, ' linages evolved'
     print '\nliving linages:'
     for each in living_linages:
         print 'linage', each[0], 'born', each[1], 'morphology:', each[3]
     print '\nextinct linages: '
     for each in dead_linages:
         print 'linage', each[0], 'born', each[1], '- died', each[2]
         print 'morphology:', each[3]
     tree = tree.replace('*','')
     print '\ntree (in New Hampshire form: )', tree

# main loop
while next_linage < max_linages:
     # handles branching
     (next_linage, living_linages, tree) = Braching(debug, next_linage,  
time, living_linages, mutation_rate, tree)
     # handles extinction
     (living_linages, dead_linages) = Extinction(debug,  
extinction_probablity, living_linages, time, dead_linages)
     if len(living_linages) == 0: break    # when the last  
living_linages goes extinct exit
     time += 1

Print_results(next_linage, max_linages, living_linages, dead_linages,  
tree)

Thank you,

Vincent

------------------------------------------------------------------------ 
--------------
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