O'Reilly logo

Python Programming On Win32 by Mark Hammond, Andy Robinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

User-Defined Validations

Imagine you’re building a set of year-end accounts, repeatedly importing data from several sources and making manual edits. It all has to be perfect, and it’s 2:00 a.m. You think you have the first half of the year sorted out and accidentally enter a transaction dated October 1998 instead of October 1999. It doesn’t show up in the View you are looking at, so you enter it again and make other corrections elsewhere based on your erroneous account balances. If you are unlucky, you could waste hours finding the error and unravelling all the dependent changes (One author knows this all too well). The userhooks file also defines a sample Validator that puts a time lock on the BookSet; this prevents any changes before a cutoff date. Here’s the code:

class DateWindowValidator(Validator):
    """An example.  Prevents changes on or before a certain date
    locking the bookset"""

    def __init__(self, aDescription, aDate):
        Validator.__init__(self, aDescription)
        self.cutoff = aDate
    
    def mayAdd(self, aTransaction):
        return (aTransaction.date > self.cutoff)
    
    def mayEdit(self, index, newTransaction):
        oldtran = self.BookSet[index]
        if oldtran.date <= self.cutoff:
            return 0
        elif newTransaction.date <= self.cutoff:
            return 0
        else:
            return 1

    def mayRemove(self, index):
        tran = self.BookSet[index]
        return (tran.date > self.cutoff)
        
    # renameAccount will not break anything

More sophisticated Validators might have beginning and ending time windows, a list of accounts not to touch, or even a user-related ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required