A library that provides code contracts and assertions.
Project description
code-contracts is a library that provides:
- Contracts
Functions that impose requirements when entering a function or method - also commonly called preconditions.
- Assertions
Functions that impose requirements in the body of a function or method. This is not a complete suite of assertions; instead, they are meant to be complementary to those available in unittest.TestCase.
The goal of this library is not to make Python a statically-typed language. Instead, it aims to help you define what is expected from your code before it executes, so that you can more easily track and prevent bugs.
So here’s how you can use contracts:
from contracts import contract
def build_rocket(name, model, company):
contract.is_not_empty(name)
contract.is_greater_than(model, 0)
contract.is_not_empty(company)
print("You built a {0} {1} rocket from {2}.".format(name, model, company))
if __name__ == "__main__":
build_rocket("Falcon", 9, "SpaceX")
And here’s how you can use assertions to unit test the above function:
import unittest
from contracts import assertion
class RocketTests(unittest.TestCase):
def test_build_rocket(self):
assertion.does_not_raise(ValueError, build_rocket, "Falcon", 9, "SpaceX")
code-contracts officially supports Python 3.3 and onwards.
Installation
Simply run the following command in your favorite terminal:
$ pip install code-contracts
Documentation
The full documentation is available at http://www.google.com/
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for code_contracts-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a28e08b2a089c7bcfb771600ce5ffa55157e42b5c8494901ba60e7419ee4dfd2 |
|
MD5 | 1fb5acec4a5ebd6c97fe031304dfd913 |
|
BLAKE2b-256 | ac53c7a43f239103fae7772bddd8d954c3afaa403cd5547242de2607a1bc31fc |