[Tutor] make code less ugly/more pythonic?
ashleigh smythe
absmythe at ucdavis.edu
Tue Dec 9 19:03:13 EST 2003
Greetings! I've written a little script to process a text file. It
does exactly what I want it to do but as I'm a newbie I'm sure it's very
ugly and brute force - I wondered if anyone had a moment to please take
a look and tell me how to improve it? For instance I have a function
that calls a function that calls a function! Is that awkard and
unpythonic? Should the whole thing be in a class - how would I do that?
The program simply parses a file that has 10,000 iterations of the
following:
Strict consensus of 1 tree:
Statistics derived from consensus tree:
Component information (consensus fork) = 185 (normalized = 0.964)
Nelson-Platnick term information = 3855
Nelson-Platnick total information = 4040
Mickevich's consensus information = 0.183
Colless weighted consensus fork (proportion max. information) =
0.216
Schuh-Farris levels sum = 0 (normalized = 0.000)
Rohlf's CI(1) = 0.989
Rohlf's -ln CI(2) = 948.111 (CI(2) = 0.00)
I want to extract that Component information (consensus fork) value (185
in this instance) from each of the 10,000 "paragraphs" of data in the
file and get an average of that value.
Below is the code I hacked together- please don't laugh!!! :-)
Thanks for any imput,
Ashleigh
import re
import sys
import string
def make_int(mylist): #turns the list of consensus
an_int_list=[int(n) for n in mylist] #fork values from strings to
return an_int_list #integers so I can average
#them.
def add_up(mylist): #calls function to turn
int_list=make_int(mylist) #strings into
asum=0 #integers.
for x in int_list: #sums up all the values
asum +=x
return asum
def average(mylist): #takes the sum of consensus
sum=add_up(mylist) #fork values and finds their
length=len(mylist) #average.
average_confork=sum/length
return average_confork
mainlist=[] #initialize the list of
#consensus fork values.
def search(file_to_search):
infile=open(file_to_search, 'r')
for line in infile:
if re.search('Component', line): #searches the input file for
confork=line[45:49] #line containing cons. fork,
mainlist.append(confork) #removes the value and puts
return mainlist #it into a list.
def aveconfork(file_to_search): #this executes the whole thing
thelist=search(file_to_search) #so I start the program with
finalaverage=average(mainlist) #aveconfork.aveconfork
return finalaverage #('file_to_search')
More information about the Tutor
mailing list