Evaluate my first python script, please
pemerson at gmail.com
Fri Mar 5 16:53:06 CET 2010
Thanks for your response, further questions inline.
On Mar 4, 11:07 am, Tim Wintle <tim.win... at teamrubber.com> wrote:
> 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__":
Is this purely aesthetic reasons, or will I appreciate this when I
write my own modules, or something else?
> (2) PEP8 (python style guidelines) suggests one import per line
> (3) I'd use four spaces as tab width
> 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
Ah, yes, I like that. It moves towards the set notation I've wound up
with. Definitely more readable to me.
> (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:]):
This doesn't actually work, because I'm not looking for a one to one
mapping of args to sections of hostname, but rather a subset. So
passing in 'prod sfo' would register a match for '001.webapp.prod.sfo'.
More information about the Python-list