Skip to main content

A library that provides code contracts and assertions.

Project description

code-contracts is a library that provides:

  1. Contracts

    Functions that impose requirements when entering a function or method - also commonly called preconditions.

  2. 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://contracts.readthedocs.io/.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

code-contracts-0.1.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

code_contracts-0.1.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file code-contracts-0.1.1.tar.gz.

File metadata

File hashes

Hashes for code-contracts-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d3682eb82fa7084a4db287cce9219170105dda52b00a3196e19fd1b61b4a278f
MD5 d2f65ba937251ba631586cf5833ccd28
BLAKE2b-256 5d40a18f6b363712419292b19f8d119a2690447333c3d8e4515c99801414639e

See more details on using hashes here.

File details

Details for the file code_contracts-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for code_contracts-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 127091b03c3a58c155d034a57ad5eade105598340d49f42c764abc612667a2d6
MD5 08c98258a4c64ab9c6dcdfa28c8bd9fa
BLAKE2b-256 0f84b8f13edc5506d85fd013ac0de08befe0b88980810a9639a44b4edb6f1fab

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page