Evaluate my first python script, please

Tim Wintle tim.wintle at teamrubber.com
Thu Mar 4 14:07:24 EST 2010


On Thu, 2010-03-04 at 10:39 -0800, Pete Emerson wrote:
> I am looking for advice along the lines of "an easier way to do this"
> or "a more python way" (I'm sure that's asking for trouble!) or
> "people commonly do this instead" or "here's a slick trick" or "oh,
> interesting, here's my version to do the same thing".

(1) I would wrap it all in a function

def main():
    # your code here

if __name__ == "__main__":
    main()

(2) PEP8 (python style guidelines) suggests one import per line

(3) I'd use four spaces as tab width

(4) 
I'd change this:

>     for arg in sys.argv[1:]:
>         for section in hostname.split('.'):
>             if section == arg:
>                 count = count + 1
>                 break

to something more like:

    for section in hostname.split("."):
        if section in sys.argv[1:]:
            count += 1

(although as you suggested I'd only calculate sys.argv[1:] once)

... or you could replace whole section between the for loop and
hosts.append with:

    if sorted(hostname.split(".")) == sorted(sys.argv[1:]):
        host.append(hostname)


, at a slight loss of clarity - but I think I'd stick with the more
verbose version personally.

Tim






More information about the Python-list mailing list