Please critique my script

Gerald Britton gerald.britton at gmail.com
Thu Jul 14 20:44:04 CEST 2011


For me, there are some things I don't like much.  One-character
variable names stand out (tend to make the code hard to read).
Violation of PEP 8 guidelines, especially wrt spacing.  e.g.

result.append("%s[%s]%s" % (lastprefix, tails, lastsuffix))

not

result.append("%s[%s]%s"%(lastprefix,tails,lastsuffix))

Similarly for many of the assignments and expressions.

I see that you initialize count3 at line 39 but don't use it until
line 123.  I'd suggest that you move the initialization closer to its
use.

I think that you should move the call to open closer to where you're
going to write to the file.  In fact, you can do the thing a bit
neater with a context manager like this:

#------Print the dial-peers to file----
with open(x, 'w') as o:
    for line in catlist2:
        figureDpn = count3 + 1000
        dpn = str(figureDpn)
        label = "dial-peer voice " + dpn
        o.write(label)
        o.write('\n')

...

Note that if you use this approach you don't need a separate call to close().

Also, you can do all the writing in one call to write(), something like this:

        o.write(
            label + '\n' +
            "description *** local outbound dialpeer ***" + '\n' +
            destpatt + '\n' +
            "port " + p + '\n'
            "forward-digits 7" if line[0:3] == y and q == "y" else ""
            '\n'
            )

Which keeps the eye focused on the data being written (at least for me).

-- 
Gerald Britton



More information about the Python-list mailing list