[Tutor] Style/Conceptual Help

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Nov 2 20:15:20 CET 2005



On Wed, 2 Nov 2005 ebgreen at customdatasoft.com wrote:

> I would be interested in some input/suggestions on the pythonicness of
> the way I did things and on style, or anything else you see that I can
> improve.

Hi ebgreen,

The only big thing I'd point out is that the buttonClick() method of the
application is too big.  *grin*


You may want to extract some of its functionality as separate methods. The
overall movement of button1Click appears to be the following:

### Pseudocode #####################
def button1Click():
    importConfigurationInformation()
    getListOfFiles()
    copyFilesToTargetDirectory()
    deleteSourceFilesSafely()
####################################

I'm boxing things this way initially because the program's comments seem
to favor it.


The configuration information stuff might want to live in a separate
class: I'm not sure if the GUI itself should be so intimately involved
with the details of reading off a ConfigParser.  One possibility is to do
something like this:


##################################################################
class Configuration:
    def __init__(self, configfile):
        parser = ConfigParser.ConfigParser()
        parser.read(configfile)
        self.sourcedirloc = parser.get("GetPics", "sourcedir")
        self.targetdirloc = parser.get("GetPics", "targetdir")
        self.deleteold = parser.get("GetPics", "deleteold")
        self.extList = parser.get("GetPics",
                                  "extensions").lower().split(",")
##################################################################

(For the moment, I'm stripping out the exception handling just to make
this example simple:  you'll probably want to add it back later.)


If we have something like this Configuration object, then we can use it
as:

###########################################
def button1Click(self, event):
    files = {}
    nameList = []
    config = Configuration("./GetPics.ini")

    #OPen the source dir and get a list of the files making the new name and
    #path as you go
    sourcefiles = os.listdir(config.sourcedirloc)
    ...
###########################################


That is, if we're careful about things, we can slowly extract blocks of
code into separate classes and methods, and we'll make the code easier to
read.

The extraction above also makes it easier to see that it might even be
possible to reorder the configuration reading part to some time even
before buttons are clicked, since it doesn't really pay attention to
anything else besides the filename './GetPics.ini'.


Hope this helps!



More information about the Tutor mailing list