Please Critique

Bernhard Reiter bernhard at alpha1.csd.uwm.edu
Fri Dec 10 16:17:07 EST 1999


On 7 Dec 1999 02:32:04 GMT, Michael Fraley <fraley at usfca.edu> wrote:

>This is my first real-world Python script, I'd like to hear any
>pointers on how to improve it or do things better. 

>Here's my script. I'd really appreciate any pointers on how to 
>improve it.

Add documentation to it. (Docstrings and some lines seperating blocks.)
Use the (standard) fileinput module.
You might take advantage of the range(function) to do the splitting.
	Something like for keyposition in range (0,len(trimline),4):

Bernhard


.import string
.screenfile = open('scracc.txt')
.newfile = open('scrnew.txt', "w")
.for line in screenfile.readlines():
.    mydict = {}
.    trimline = line[4:-1]
.    start = 0
.    length = len(trimline)
.    while start < length:
.	end = start + 4
.	startkey = start + 1
.	mydict[trimline[startkey:end]] = trimline[start:end]
.	start = end
.    sortkeys = mydict.keys()
.    sortkeys.sort()
.    mylist = []
.    for key in sortkeys:
.	mylist.append(mydict[key])
.    newline = line[0:4] + string.joinfields(mylist, "") + "\n"
.    newfile.write(newline)
.screenfile.close()
.newfile.close()

	
-- 
Research Assistant, Geog Dept UM-Milwaukee, USA.  (www.uwm.edu/~bernhard)
Free Software Projects and Consulting 		         (intevation.net)  
Association for a Free Informational Infrastructure            (ffii.org)



More information about the Python-list mailing list