Programming by contract
Project description
Deal – python library for design by contract (DbC) programming.
That’s nice assert statements in decorators style to validate function input, output, available operations and object state. Goal is make testing much easier and detect errors in your code that occasionally was missed in tests.
Features
Functional declaration.
Custom exceptions.
Raising exceptions from contract.
Django Forms styled validators.
Attribute setting invariant validation.
Dynamically assigned attributes and methods invariant validation.
Decorators to control available resources: forbid input/output, network operations, raising exceptions
Available decorators
Installation
pip3 install --user deal
Quick Start
import re
import attr
import deal
REX_LOGIN = re.compile(r'^[a-zA-Z][a-zA-Z0-9]+$')
class PostAlreadyLiked(Exception):
pass
@deal.inv(lambda post: post.visits >= 0)
class Post:
visits: int = attr.ib(default=0)
likes: set = attr.ib(factory=set)
@deal.pre(lambda user: REX_LOGIN.match(user), message='invalid username format')
@deal.raises(PostAlreadyLiked)
@deal.chain(deal.offline, deal.silent)
def like(self, user: str) -> None:
if user in self.likes:
raise PostAlreadyLiked
likes.add(user)
@deal.post(lambda result: 'visits' in result)
@deal.post(lambda result: 'likes' in result)
@deal.post(lambda result: result['likes'] > 0)
@deal.pure
def get_state(self):
return dict(visits=self.visits, likes=len(self.likes))
Now, Deal controls conditions and states of the object at runtime:
@deal.inv controls that visits count in post always non-negative.
@deal.pre checks user name format. We assume that it should be validated somewhere before by some nice forms with user-friendly error messages. So, if we have invalid login passed here, it’s definitely developer’s mistake.
@deal.raises says that only possible exception that can be raised is PostAlreadyLiked.
@deal.chain(deal.offline, deal.silent) controls that function has no network requests and has no output in stderr or stdout. So, if we are making unexpected network requests somewhere inside, deal let us know about it.
deal.post checks result format for get_state. So, all external code can be sure that fields likes and visits always represented in the result and likes always positive.
If code violates some condition, sub-exception of deal.ContractError will be raised:
p = Post()
p.visits = -1
# InvContractError:
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
File details
Details for the file deal-2.4.0.tar.gz
.
File metadata
- Download URL: deal-2.4.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1f454b97083b4b7fd0f2ef982885ec45840edf91bb15a4d4abb5df755480280 |
|
MD5 | 95ddd16d48ea5ec3d8fd69a42af4b4fb |
|
BLAKE2b-256 | bc1dccf7d2d89ca682c4ef4a77c97dc3053af232b8aa5ff9b2283add71921bb2 |
File details
Details for the file deal-2.4.0-py3-none-any.whl
.
File metadata
- Download URL: deal-2.4.0-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0ffee7b5fd9c5c11032425b3de5a348715355651b3009ffa460beadce517535 |
|
MD5 | 3ed260d179d93aaf8445141dd8509c0a |
|
BLAKE2b-256 | d77641d0978b115fab9c45e20aebf5582566e2d2d1473ce664503df4a95a1aa0 |