I’m still bad at Python, and probably programming, too.

I know it’s bad to duplicate code, but I’m stumped on finding a more elegant solution. The following four functions are all nearly identical, but do different things.

<pre>    def CasesOpenedBeforeDate(self, beforedate = None):
        if beforedate == None:
            pass
        else:
            self.tempcaselist = []
            beforemonth, beforeday, beforeyear = beforedate.split('/')
            beforedate = date(int(beforeyear), int(beforemonth), int(beforeday))
            for acase in self.caselist:
                casemonth, caseday, caseyear = acase['Date Added'].split('/')
                casedate = date(int(caseyear), int(casemonth), int(caseday))
                if casedate < beforedate:
                    self.tempcaselist.append(acase)
            self.caselist = self.tempcaselist

    def CasesOpenedAfterDate(self, afterdate = None):
        if afterdate == None:
            pass
        else:
            self.tempcaselist = []
            aftermonth, afterday, afteryear = afterdate.split('/')
            afterdate = date(int(afteryear), int(aftermonth), int(afterday))
            for acase in self.caselist:
                casemonth, caseday, caseyear = acase['Date Added'].split('/')
                casedate = date(int(caseyear), int(casemonth), int(caseday))
                if casedate > afterdate:
                    self.tempcaselist.append(acase)
            self.caselist = self.tempcaselist

    def CasesClosedBeforeDate(self, beforedate = None):
        if beforedate == None:
            pass
        else:
            self.tempcaselist = []
            beforemonth, beforeday, beforeyear = beforedate.split('/')
            beforedate = date(int(beforeyear), int(beforemonth), int(beforeday))
            for acase in self.caselist:
                casemonth, caseday, caseyear = acase['Date Closed'].split('/')
                casedate = date(int(caseyear), int(casemonth), int(caseday))
                if casedate < beforedate:
                    self.tempcaselist.append(acase)
            self.caselist = self.tempcaselist

    def CasesClosedAfterDate(self, afterdate = None):
        if afterdate == None:
            pass
        else:
            self.tempcaselist = []
            aftermonth, afterday, afteryear = afterdate.split('/')
            afterdate = date(int(afteryear), int(aftermonth), int(afterday))
            for acase in self.caselist:
                casemonth, caseday, caseyear = acase['Date Closed'].split('/')
                casedate = date(int(caseyear), int(casemonth), int(caseday))
                if casedate > afterdate:
                    self.tempcaselist.append(acase)
            self.caselist = self.tempcaselist</pre>

The way WordPress formatted the code above might be wonky, so here is a Gist – https://gist.github.com/3836116

Those functions prune a list of support cases down to only those that were opened or closed before or after a certain date. The only differences between the functions are the dict key it looks at, and the comparison of whether or not that value is before or after the date passed to the function.

A good programmer (or someone more experience with Python) would know how to make it one function. I could pull it off in C, but I can’t figure out a “Pythonic” way of doing it.

This entry was posted in Hobbies, Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s