Skip to main content

simple, human readable decorators package to ensure your method abides to it's contract

Project description

Simple, human readable decorators to ensure your method abides to it's contract for Python

Rules

Rule sa_int sa_float sa_bool sa_string
not zero non_zero (bool) non_zero (bool)
greater than gt (int) gt (float)
greater than or equal gte (int) gte (float)
lesser than lt (int) lt (float)
lesser than or equal lte (int) lte (float)
modulo mod (int) mod (float)
not empty not_empty (bool)
not blank not_blank (bool)
ends with ends_with (string)
starts with starts_with (string)
contains contains (string)
is lower is_lower (string)
is upper is_upper (string)
regex regex (string)
Rule Description
not zero ensure the argument does not equal 0.
with rule non_zero=True and call(0) => Exception
with rule non_zero=True and call(1) => Accepted
greater than ensure the argument is greater than the rule value.
with rule gt=3 and call(2) => Exception
with rule gt=3 and call(3) => Exception
with rule gt=3 and call(4) => Accepted
greater than or equal ensure the argument is greater than, or equal to the rule value.
with rule gte=3 and call(2) => Exception
with rule gte=3 and call(3) => Accepted
with rule gte=3 and call(4) => Accepted
lesser than ensure the argument is lesser than the rule value.
with rule lt=3 and call(2) => Accepted
with rule lt=3 and call(3) => Exception
with rule lt=3 and call(4) => Exception
lesser than or equal ensure the argument is lesser than, or equal to the rule value.
with rule lte=3 and call(2) => Accepted
with rule lte=3 and call(3) => Accepted
with rule lte=3 and call(4) => Exception
modulo ensure the argument is a multiple of the rule value.
with rule mod=4 and call(6) => Exception
with rule mod=4 and call(4) => Accepted
with rule mod=4 and call(-4) => Accepted
with rule mod=4 and call(0) => Accepted
not empty ensure the argument is not an empty string.
with rule not_empty=True and call('') => Exception
with rule not_empty=True and call(' ') => Accepted
with rule not_empty=True and call('abc') => Accepted
not blank ensure the argument is not an empty string, or contains only whitespace characters, according to Python String.isspace().
with rule not_empty=True and call('') => Exception
with rule not_empty=True and call(' ') => Exception
with rule not_empty=True and call('\t \n ') => Exception
with rule not_empty=True and call('abc') => Accepted
ends with ensure the argument ends with the rule value, according to Python String.endswith().
with rule ends_with='bc' and call('abcd') => Exception
with rule ends_with='bc' and call('abc') => Accepted
with rule ends_with='1' and call('1') => Accepted
with rule ends_with='' and call('') => Accepted
starts with ensure the argument starts with the rule value, according to Python String.startswith().
with rule starts_with='ab' and call('1ab') => Exception
with rule starts_with='ab' and call('') => Exception
with rule starts_with='ab' and call('abc') => Accepted
with rule starts_with='ab' and call('ab123') => Accepted
contains ensure the argument contains the rule value, according to Python String.find().
with rule starts_with='ab' and call('a b') => Exception
with rule starts_with='ab' and call('a1b') => Exception
with rule contains='ab' and call('abcd') => Accepted
with rule starts_with='ab' and call('12ab34') => Accepted
is lower ensure all non-whitespace characters in the argument are lowercase.
with rule is_lower=True and call('ABC') => Exception
with rule is_lower=True and call('aBc') => Exception
with rule is_lower=True and call('abc') => Accepted
with rule is_lower=True and call(' ! ') => Accepted
with rule is_lower=True and call('') => Accepted
is upper ensure all non-whitespace characters in the argument are uppercase.
with rule is_upper=True and call('abc') => Exception
with rule is_upper=True and call('AbC') => Exception
with rule is_upper=True and call('12AB34') => Accepted
with rule is_upper=True and call(' ! ') => Accepted
with rule is_upper=True and call('') => Accepted
regex ensure the argument matches the regex using Python re.search().
with rule regex='test[12]' and call('test3') => Exception
with rule regex='test[12]' and call('test2') => Accepted
with rule regex='test[12]' and call('test1') => Accepted

Demo

Want a demo of other rules? check out the test directory, it has an example for every rule there is!

@sa_int('val', gt=0, lte=10)
def assign_score(val)
    ""assign an integer score higher than 0, up to 10""
    print('set score to {val}')
    score = val

assign_score(5)

assign_score(0)
> int argument val with value 0 was not greater than 0

assign_score('abc')
> int argument val with value 'abc' was of type string, not of type 'int'

assign_score(None)
> int argument val was None

assign_score(3.5)
> int argument val with value 0 was not greater than 0

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

pytsa-0.1.tar.gz (8.6 kB view hashes)

Uploaded Source

Built Distribution

pytsa-0.1-py3-none-any.whl (11.2 kB view hashes)

Uploaded Python 3

Supported by

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