[Tutor] Hi I need advice

Jeff Shannon jeff@ccvcorp.com
Wed, 07 Nov 2001 09:57:26 -0800


> From: "Edy Lie" <ed@iterunet.com>:
>
> Hi everyone.
>
>    I need some help/advice over the following code. My 1st attempt using
> class.

Well, someone else has already shown you the joy of default values.  However, a
bit more extensive advice...  You've got a good start, but you could use your
class much more effectively.  For one thing, when you're creating a new DataBase
instance, you can pass it data that you've already collected.  This is generally
considered cleaner than creating the instance and then stuffing data into it.
But the big advantage to having a class, is that you can add methods to it that
manipulate its data.  Since the method is *part* of the class, it already knows
how it should be used, and then the rest of your code doesn't have to care.  So,
for instance, your code for writing a DataBase() record to a file, should be
part of the DataBase class.  Try the following:

class DataBase:
    def __init__(self, plan='', domain='', email='', name=''):
        self.UserPlan = plan
        self.UserDomain = domain
        self.UserEmail = email
        self.UserName = name
    def save(self, filename='record.txt'):
        # Note that we've got a default filename, so unless
        # you specify otherwise, all records will go in this file.
        f = open(filename,'a')
        # Also note that I'm appending to the end of the file,
        # instead of replacing the file as you did originally.
        f.write("Plan: %s\nDomain: %s\nEmail: %s\nName: %s\n-----\n" %
                             (add.UserPlan, add.UserDomain,
                              add.UserEmail, add.UserName))
        f.close()   # It's a good idea to close files when you're done with them

Now, in your menu code, you can do this:

if choice == 1:
    print 'Adding New User\n'
    myplan = raw_input("Plan? ")
    mydomain = raw_input("Domain? ")
    myemail = raw_input("Email address? ")
    myname = raw_input("Name? ")
    add = DataBase(myplan, mydomain, myemail, myname)
    save = raw_input("Do you want to save?  (y/n):  ")
    save = save.lower()  # We don't care if it's caps or not
    if save == "y":
        add.save()
        print "Record saved.\n"
    elif save == "n":
        print "No data saved.\n"
    else:
        print "Wrong data--exiting.\n"
        raise SystemExit
        # Raising the SystemExit exception is almost the same as
        # calling sys.exit(), but doesn't require importing sys,
        # and leaves the option of catching the exception.

See how the DataBase object takes care of saving itself?  The whole point of
classes, is that they know how to handle all their own stuff.  Ideally, anything
that is normally done *to* the object, should be moved to become *part* of the
object.  So, we could also add a method to DataBase that will get input and fill
out its own data.  We could also add another method that would read in the file
that we've just written out, and initialize itself from that.  And so on, and so
on.  You could also create a Menu class, say, that has a dictionary of options,
and functions to call if that option is selected, to make *that* part of your
program a little cleaner.  The possibilities are endless.    :)

Hope that this gives you something to think about, and good luck!

Jeff Shannon
Technician/Programmer
Credit International