[Tutor] Re: Is there a "better" way to do this?

Lee Harr missive at hotmail.com
Wed Dec 22 23:50:58 CET 2004


class greating:
    # I think the word you are looking for is
    # "greeting"

    def __init__(self):
        self.OK = False
        self.lowValue = 1
        self.highValue = 6

    def opening(self):
        print """
        Please choose from the following options.
        1) - Normal Unit test with static data.
        2) - Normal Unit test with missing data.
        3) - Integration test with current DTG.
        4) - Integration test with missing data.
        5) - Clean directory
        6) - Exit
        """
        self.choice = raw_input("Choice(1-6) ")

    # I would probably do this more like ...
    def set_options(self):
        self.options = ['Normal Unit test with static data',
                                'Normal Unit test with missing data',
                                # ... etc
                                 ]

    def opening2(self):
        for i, option in self.options:
            print '%s) - %s', (i+1, option)


# it makes the code marginally more complex now
# but think about what will happen when you go to
# rearrange the options or add new ones ...
#
# also, think about what you are going to "do" with
# the choice made ... you might make the options
# list a list of tuples and handle it all in one place ...

    def normal_with_static(self):
        pass

    def normal_with_missing(self):
        pass

    def set_options2(self):
        self.options = [('Normal Unit test with static data', 
self.normal_with_static),
                                ('Normal Unit test with missing data', 
self.normal_with_missing),
                                # ... etc
                                 ]

# Or if you want "object oriented" practice... how about
# an "Option" class with "description" and "method"
# attributes?  Just a thought ...

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/



More information about the Tutor mailing list