Please Critique
Michael Fraley
fraley at usfca.edu
Mon Dec 6 21:32:04 EST 1999
Hello,
This is my first real-world Python script, I'd like to hear any
pointers on how to improve it or do things better.
I used it to organize screen access to one of our production systems.
First I dumped the screen access into a text file. The lead four bytes
are the operator number in question, followed by four bytes for each
screen (or wild card ~) and the access. Example -
0007D102A204I~~~
This means for operator '0007', deny screen 102, allow screen 204,
and inquire only on everything else ('~~~')
My python script sorts on screen number and writes out a neatly sorted
list. The input is all jumbled up, from years of user updates:
sample scracc.txt input
-----------------------
0006D900DWEBA~~~A023
0361D307D023A100A001DWEBI023A~~~
0733D006A~~~I004
Here's how it should look when straightened out
sample scrnew.txt output
-----------------------------------------------
0006A023D900DWEBA~~~
0361A001I023A100D307DWEBA~~~
0733I004D006A~~~
Here's my script. I'd really appreciate any pointers on how to
improve it.
scracc.py
-----------------------------------------------------------
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()
--
Michael Fraley
fraley at usfca.edu
http://www.usfca.edu/~fraley
More information about the Python-list
mailing list