Converting from perl
Michael Hudson
mwh21 at cam.ac.uk
Tue Aug 24 18:07:51 EDT 1999
sp00fD <sp00fD at yahoo.com> writes:
> I'm converting to python from perl and just created my first python
> program, it's actually a rewrite of a perl program that I had. It's
> kind of stupid, but I find rewriting things in other languages is one of
> the best ways to learn. What I'd like is for someone to tell me what's
> screwed up and what I need to work on. The majority of the script is
> held in a class, but being a newbie to the world of OOP also, I'm not
> sure if the way I've handled things is optimal, so _any_ suggestions
> would be appreciated, as well as any "Why the hell did you create a
> class that way"'s or "You're all screwed up, you need to change
> this.."'s. Anyway, here it is...
Looks OK to me. A couple of small points...
> def get_hosts(self):
> if self.args() == "AIX":
> self.hosts = ("athena", "dev", "detroit", "la",
> \
> "laredo", "miami", "newark",
> "sat", \
> "savannah", "seattle", "titan")
> elif self.args() == "SUN":
> self.hosts = ("eclipse", "hercules", "scdev", \
> "scprod", "sol")
> elif self.args() == "BACKUP":
> self.hosts = ("eclipse", "hercules", "scdev", \
> "scprod", "sol", "athena")
> else:
> self.hosts = self.ask_for_hosts()
It might be easier to do this using a dictionary:
arg2hosts = {
"AIX":("athena","dev","detroit","la","laredo","miami",
"newark","sat","savannah","seattle","titan"),
"SUN":("eclipse", "hercules", "scdev","scprod","sol"),
"BACKUP":("eclipse", "hercules", "scdev","scprod", "sol", "athena")
}
try:
self.hosts = arg2hosts[self.args()]
except KeyError:
self.hosts = self.ask_for_hosts()
Your code could execute self.args() reoeatedly which is unecessary.
>
> def copy(self):
> _stuff = ()
> _stuff = self.files()
>
> try:
> for host in self.get_hosts():
> print "Sending %s to %s %s" % \
> (_stuff[0], host, _stuff[1])
>
> os.system("rcp %s %s:%s" % \
> (_stuff[0], host, _stuff[1]))
> except os.error:
> print "Error during system() function - oops"
Note that this will not catch errors in executing the command passed
to os.system - you need to look at the return code for that.
> c = CopyClass()
>
> c.copy()
To be honest, I don't see a lot of value in wrapping this up in a
class. For a straight through shell scripty type job like this I
generally don't bother. Python is not about ramming methodologies down
your throat.
> </PRE>
>
> Hope that PRE works
Hmm, seemed unecessary here...
HTH,
Michael
More information about the Python-list
mailing list