Converting from perl

sp00fD sp00fD at yahoo.com
Tue Aug 24 19:14:55 CEST 1999


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

<PRE>

#!/usr/bin/env python

import sys
import os

class CopyClass:

        def __init__(self):
                self.hosts = ()
                self.src = None
                self.dst = None

        def usage(self):
                print """Usage: %s -p [preset]
                where preset =
                        AIX     : athena dev detroit la laredo
                                  miami newark sat savannah
                                  seattle titan
                        SUN     : eclipse hercules scdev
                                  scprod sol
                        BACKUP  : athena eclipse hercules
                                  scdev scprod sol xena
                        OTHER   : You choose""" % sys.argv[0]

        def args(self):
                import getopt

                _ok = 1

                try:
                        opts, args = getopt.getopt(sys.argv[1:], 'p:')
                except getopt.error:
                        _ok = 0

                if not _ok or len(args) != 2:
                        self.usage()
                        sys.exit(1)

                for o, a in opts:
                        if o != '-p':
                                self.usage()
                                sys.exit(1)
                        _preset = a

                return _preset

        def files(self):
                if os.path.isfile(sys.argv[3]):
                        self.src = sys.argv[3]

                self.dst = sys.argv[4]

                if self.src and self.dst:
                        return(self.src, self.dst)
                else:
                        self.usage()
                        sys.exit(1)


        def ask_for_hosts(self):
                import string

                self.hosts = string.splitfields(raw_input("Enter hosts:
"))
                return self.hosts

        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()

                return self.hosts

        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"

c = CopyClass()

c.copy()

</PRE>

Hope that PRE works

thanks


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




More information about the Python-list mailing list