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) => Exceptionwith 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) => Exceptionwith rule gt=3 and call(3) => Exceptionwith 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) => Exceptionwith rule gte=3 and call(3) => Acceptedwith 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) => Acceptedwith rule lt=3 and call(3) => Exceptionwith 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) => Acceptedwith rule lte=3 and call(3) => Acceptedwith 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) => Exceptionwith rule mod=4 and call(4) => Acceptedwith rule mod=4 and call(-4) => Acceptedwith rule mod=4 and call(0) => Accepted |
not empty | ensure the argument is not an empty string. with rule not_empty=True and call('') => Exceptionwith rule not_empty=True and call(' ') => Acceptedwith 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('') => Exceptionwith rule not_empty=True and call(' ') => Exceptionwith rule not_empty=True and call('\t \n ') => Exceptionwith 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') => Exceptionwith rule ends_with='bc' and call('abc') => Acceptedwith 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') => Exceptionwith rule starts_with='ab' and call('') => Exceptionwith rule starts_with='ab' and call('abc') => Acceptedwith 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') => Exceptionwith rule starts_with='ab' and call('a1b') => Exceptionwith rule contains='ab' and call('abcd') => Acceptedwith 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') => Exceptionwith rule is_lower=True and call('aBc') => Exceptionwith rule is_lower=True and call('abc') => Acceptedwith 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') => Exceptionwith rule is_upper=True and call('AbC') => Exceptionwith rule is_upper=True and call('12AB34') => Acceptedwith 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') => Exceptionwith rule regex='test[12]' and call('test2') => Acceptedwith 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
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
pytsa-0.1.tar.gz
(8.6 kB
view hashes)
Built Distribution
pytsa-0.1-py3-none-any.whl
(11.2 kB
view hashes)