Skip to main content

Design by Contract with Functional Programming

Project description

ensures

ensures is a simple Python package that implements the idea of Design by Contract described in the Pragmatic Paranoia chapter of The Pragmatic Programmer. That's the chapter where they say you should trust nobody, not even yourself.

Main Features

  • Verification of lists of pre/post condition and invariant functions.
  • Usage of arbitrary functions for such verification.
  • Result-type return values.

Installation

pip install ensures

Usage

precondition / require

Runs a list of functions on all args.

Returns Error if any of them fails.

from ensures import precondition


def is_positive(x):
    """Check if a number is positive."""
    return x > 0


@precondition(is_positive)
def square_root(x):
    """Calculate square root with precondition that x must be positive."""
    return x**0.5

postcondition / ensure

Runs a list of functions on the result.

Returns Error if any of them fails.

from ensures import ensure


def result_is_even(result):
    """Check if result is even."""
    return result % 2 == 0


@ensure(result_is_even)  # Using the alias
def double_number(x):
    """Double a number with postcondition that result is even."""
    return x * 2

invariant

Runs a list of functions on all args.

Returns Error if any of them fails.

from ensures import invariant


@invariant(lambda x: x >= 0)  # Simple lambda invariant
def increment_counter(x):
    """Increment a counter with invariant that it stays non-negative."""
    return x + 1

Result Handling

Pattern matching is supported to unpack the Return value.

from ensures import Error, Success


result1 = square_root(1)
result2 = square_root(-1)  # This will return an Error instance

def handle_result(res):
    match res:
        case Success(value):
            print(f"Square root calculated: {value}")
        case Error(func, args):
            print(f"Precondition failed in {func.__name__} with args {args}")

handle_result(result1)
handle_result(result2)

More examples

Check examples.py

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

ensures-0.1.3.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ensures-0.1.3-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

Details for the file ensures-0.1.3.tar.gz.

File metadata

  • Download URL: ensures-0.1.3.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.2

File hashes

Hashes for ensures-0.1.3.tar.gz
Algorithm Hash digest
SHA256 4a3f2ca8c049093ca69a16a9fadeff91228070dabb3a58c281212f89d06a1d10
MD5 074e3d19a36263403025e85b43e74379
BLAKE2b-256 eef04a53d876cfa9d44b172880f66731ec4085d6a1a7d9e7ab8582e7d65861dc

See more details on using hashes here.

File details

Details for the file ensures-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: ensures-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 18.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.2

File hashes

Hashes for ensures-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bfa098d33a06ddbe93bf814b3653917f6a204310c4445bda1dee7b03c4c44c1a
MD5 785b4aca3f5e64f2e1568adb4dd068f5
BLAKE2b-256 4d8b3d5ad9cc92c06e4d8e858c61256d6f036ed1f1584dc1e8ac978a8d8bf81a

See more details on using hashes here.

Supported by

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