Please Critique

Robert Roy rjroy at takingcontrol.com
Thu Dec 16 12:37:13 EST 1999


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

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

This is a different approach using regular expressions and a sort
function. Much terser though somewhat slower (about 2.5x) because of
the re and the call to the sort function. 

import re
import string
reFIND4= re.compile('....')
outfile = open('srcaccessnew.txt', 'w')
for line in open('scraccess.txt').readlines():
    l=reFIND4.findall(line)
    l.sort(lambda x,y: cmp(x[1:4],y[1:4]))
    outfile.write(string.join(l,'') + '\n')
outfile.close()




More information about the Python-list mailing list