Wednesday 25 April 2012

Initialise!

Hi everyone.

I'm starting a weblog. I guess every blog needs a raison d'etre: mine is to keep a log of my progress in programming, specifically the making of web scripts. My goal is to make it stay positive, and maybe throw in a few interesting diversions now and again. And keep it informal too!  :)

In my day job, I'm a developer writing web applications using the Python and PHP languages. Associated with these are databases like MySQL and MongoDB, and frameworks like Flask (for Python) and jQuery for Javascript. There are also web servers like Apache and Cherokee, and code repositories like Subversion and Mercurial. I spend all my days in Ubuntu Linux and my applications are deployed on Linux servers. I've been doing it full-time for 17 months, now.

OK, so what does that really mean? Well, dealing with all this stuff is challenging. The pace of change in Open Source software is very fast, and I have to keep up with things. I'm using things at a high level, which saves time and leads to rapid development. I enjoy what I do usually, and always have a hope that I'll turn into a really good programmer, one of these days.

My challenge the last week has been unit testing. I've done small sets of test functions before, but suddenly I had an urgent need to test a bunch of functions for a recurring financial transaction. This transaction had to reoccur automatically once a month for a fixed duration, e.g. 12 months. What I also had to contend with was some convoluted logic, which involves the approving of the transaction before it is allowed to recur, and also the fact that the recurring start date can be different from the initial transaction's creation date! Confused yet? Well to be honest, so was I.  ;-)

This is being done in Python through the unittest module. What I'm doing is creating a setUpClass with a decorator instead of a plain setUp method, so I can have two variables available to all my test functions. One is just a log file. The other,  "self.rows" is a list of transactions with database ids for checking later.

Here's a snippet:

class Tests(unittest.TestCase):
    """ """
    @classmethod 
    def setUpClass(self):
        """ """
        self.rows = []

        ...
        self.log = open(log_name, "w")

I am getting used to having a library file full of my functions to test, and importing it. Then I'm going to be doing lots of asserts on return values based on test scenarios, I suppose. Now I have sorted out the knotty transaction logic, I'm on the downhill slope. Yahoo!

My goal after all this is to be able to reuse the library functions I'm testing, for a scheduled script that will run daily and create the recurring transactions. (No, this won't be through a cron job!) It will be running through a cron-like function from the uwsgi module for Python web apps, e.g.

from uwsgidecorators import cron

@cron(0, 3, -1, -1, -1)
def gen_recurring_transaction(signal):
    ...

I'm lucky I have a vastly experienced person available who also helped me with those two snippets above. Time to keep on learning these great tricks! Till next time.