Package: contracts
Contracts For Python--------------------This library is a very simple and straightforward way of implementing contractsin python using decorators. I did have a look at a few other contract librariesand found that they were either not very pythonic or decided to use strings toencode the contract logic which makes it horrible to debug or even write yourcontracts cleanly.In the hopes of creating something better I've created this library to see ifothers find it useful.Installation------------> sudo python setup.py installWill install the contracts library which you can then import with the usual'import contracts' and start using the contracts library immediately.or install directly from github with:pip install -e git+git://github.com/rlgomes/contracts.git#egg=contractsUsage-----Function Decorators-------------------@post-----The post decorator will validate whatever you want to validate after theexecution of the decorated function and the contract library will pass thereturn value of the decorated function as your first argument. You can thenchoose to do any required validation to the result of that function or checkany other global variable state of importance.Example: return constraint check--------------------------------@post(lambda x: x > 0,"return is not a positive number")def bad_abs(a):return aThe previous function is a bad implementation of the abs function and with thepost contract that checks the return to be positive we can easily catch thatwhen we call the function with -2 as an argument this is what we'd see:>>> bad_abs(-2) Traceback (most recent call last): File "<stdin>", line 1, in <module>File "contracts.py", line 35, in new_fassert function(result), messageAssertionError: return is not a positive number@pre----The pre decorator will validate whatever you want to validate befor theexecution of the decorated function and the contract library will pass theexact arguments that are about to be passed to the decorated function beforeexecuting that.Example: argument validation@pre(lambda _,y: len(y) > 0)def find_element(elem,list):...The above example shows you can guarantee your function is never called with alist that has zero elements.Class Decorators----------------@inv----The invariant decorator will validate that the contract you want is alwaysvalidated for every interaction with any instance of the class you just decorated.Basically the framework is able to hijack all of the methods, constructors, etcin a way that makes sure that every change to the object is validated before andafter the change to not be in violation of the contract specified.@inv(lambda obj: obj.visitors >= 0)class VistorTracker(object):# if not set the pre condition will fail at initialization phase because# the attribute visitors doesn't exist at that time if removed from this# linevisitors = 0def __init__(self):self.visitors = 0def leave(self):self.visitors=self.visitors-1def visit(self):self.visitors=self.visitors+1Now basically the class VisitorTracker is constantly being checked whenever anymethod is called on the class or variable is modified the invariant contractchecks that the number of visitors is superior to 0 and that there haven'tbeen more leave() method calls executed then visit() methods called.