1 file was added to this release more than 14 days after its initial publication. Inspect the release files before installing.
--------------------
This library is a very simple and straightforward way of implementing contracts
in python using decorators. I did have a look at a few other contract libraries
and found that they were either not very pythonic or decided to use strings to
encode the contract logic which makes it horrible to debug or even write your
contracts cleanly.
In the hopes of creating something better I've created this library to see if
others find it useful.
Installation
------------
> sudo python setup.py install
Will 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=contracts
Usage
-----
Function Decorators
-------------------
@post
-----
The post decorator will validate whatever you want to validate after the
execution of the decorated function and the contract library will pass the
return value of the decorated function as your first argument. You can then
choose to do any required validation to the result of that function or check
any 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 a
The previous function is a bad implementation of the abs function and with the
post contract that checks the return to be positive we can easily catch that
when 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_f
assert function(result), message
AssertionError: return is not a positive number
@pre
----
The pre decorator will validate whatever you want to validate befor the
execution of the decorated function and the contract library will pass the
exact arguments that are about to be passed to the decorated function before
executing 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 a
list that has zero elements.
Class Decorators
----------------
@inv
----
The invariant decorator will validate that the contract you want is always
validated for every interaction with any instance of the class you just decorated.
Basically the framework is able to hijack all of the methods, constructors, etc
in a way that makes sure that every change to the object is validated before and
after 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
# line
visitors = 0
def __init__(self):
self.visitors = 0
def leave(self):
self.visitors=self.visitors-1
def visit(self):
self.visitors=self.visitors+1
Now basically the class VisitorTracker is constantly being checked whenever any
method is called on the class or variable is modified the invariant contract
checks that the number of visitors is superior to 0 and that there haven't
been more leave() method calls executed then visit() methods called.
Release files for contracts 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
File added late
1 file was uploaded more than 14 days after the first file in this release.
While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.
We recommend inspecting the release files before installing.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| contracts-0.1.0.tar.gz | 3.5 kB | Details |
Release files / contracts-0.1.0.tar.gz
File added late
This file was uploaded more than 14 days after the first file in this release.
While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.
We recommend inspecting the release file before installing.
| Download URL | contracts-0.1.0.tar.gz |
|---|---|
| Size | 3.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f5b6aa18a2a51216758caf5fc179769fe8947b54dbcda01c641d2f956c13a779
|
|
BLAKE2b-256 checksum How to use checksums |
4c89b924c4591c2aa21358b650d45f5fc09ca399075ae259a4c88be8ab131ec9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |