No project description provided
Project description
ensure is a set of simple assertion helpers that let you write more expressive, literate, concise, and readable Pythonic code for validating conditions. It’s inspired by should.js, expect.js, and builds on top of the unittest/JUnit assert helpers.
Because ensure is a standalone library (not part of a test framework), doesn’t monkey-patch anything, and doesn’t use the assert statement (which is liable to be turned off with the -O flag), it can be used to validate conditions in production code, not just for testing.
Synopsis
from ensure import ensure
ensure({1: {2: 3}}).equals({1: {2: 3}})
ensure({1: {2: 3}}).is_not_equal_to({1: {2: 4}})
ensure(True).does_not_equal(False)
ensure(1).is_an(int)
ensure(1).is_in(range(10))
ensure(range(10)).contains(5)
ensure(True).is_a(bool)
ensure(True).is_(True)
ensure(True).is_not(False)
ensure(["spam"]).contains_none_of(["eggs", "ham"])
ensure(["train", "boat"]).contains_one_of(["train"])
ensure("abcdef").contains_some_of("abcxyz")
ensure("abcdef").contains_one_or_more_of("abcxyz")
ensure("abcdef").contains_all_of("acf")
ensure("abcd").contains_only("dcba")
ensure("abc").does_not_contain("xyz")
ensure([1, 2, 3]).contains_no(float)
ensure(dict).has_attribute('__contains__')
ensure(1).is_in(range(10))
ensure("z").is_not_in("abc")
ensure(None).is_not_in([])
ensure(1).is_true()
ensure(0).is_false()
ensure(None).is_none()
ensure(1).is_not_none()
ensure("").is_empty()
ensure([1, 2]).is_nonempty()
ensure(1.1).is_a(float)
ensure(KeyError()).is_an(Exception)
ensure({x: str(x) for x in range(5)}).is_a_nonempty(dict).of(int).to(str)
ensure({}).is_an_empty(dict)
ensure(None).is_not_a(list)
import re; ensure("abc").matches("A", flags=re.IGNORECASE)
ensure([1, 2, 3]).is_an_iterable_of(int)
ensure([1, 2, 3]).is_a_list_of(int)
ensure({1, 2, 3}).is_a_set_of(int)
ensure({1: 2, 3: 4}).is_a_mapping_of(int).to(int)
ensure({1: 2, 3: 4}).is_a_dict_of(int).to(int)
ensure(10**100).is_numeric()
ensure(lambda: 1).is_callable()
ensure("abc").has_length(3)
ensure(1).is_greater_than(0)
ensure(0).is_less_than(1)
ensure(1).is_greater_than_or_equal_to(1)
ensure(0).is_less_than_or_equal_to(0)
ensure("{x} {y}".format).called_with(x=1, y=2).equals("1 2")
ensure("{x} {y}".format).with_args(x=1, y=2).is_a(str)
ensure(dict).called_with(1, 2).raises(TypeError)
with ensure().raises(ZeroDivisionError):
1/0
with ensure().raises_regex(NameError, "'w00t' is not defined"):
w00t
Raising custom exceptions
from ensure import Ensure
class MyException(Exception):
def __init__(self, e):
pass
ensure = Ensure(error_factory=MyException)
ensure("w00t").is_an(int)
def build_fancy_exception(original_exception):
return MyException(original_exception)
ensure = Ensure(error_factory=build_fancy_exception)
ensure("w00t").is_an(int)
Installation
pip install ensure
Links
Bugs
Please report bugs, issues, feature requests, etc. on GitHub.
License
Licensed under the terms of the Apache License, Version 2.0.
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
File details
Details for the file ensure-0.0.1.tar.gz
.
File metadata
- Download URL: ensure-0.0.1.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c19b2945c0bc157adbc503211e1f7abd6a2cf0fdcb4e5da251bed30298dd321 |
|
MD5 | d6f21ffc87cdc40b42c17e629ca7f57b |
|
BLAKE2b-256 | 2f192f984e227ccc98c46c428a008f6ee3943d5721803908c3c9cd0ac5297e4f |