Numeric¶
This page documents checkers for numeric arguments, as well as for sized arguments.
- class argscheck.numeric.Number(*args, **kwargs)¶
Check if x is of a numeric type (int or float) and optionally, compares it to other value(s) using any of the following binary operators: <, <=, !=, ==, >=, >.
Comparison shorthands can be used, as can be seen in the second example below.
- Parameters
other_type – Optional[Union[Type, Tuple[Type]]] – restricts the types to which x can be compared, e.g. other_type=int with ne=1.0 will raise a TypeError. By default, x can only be compared to other int or float objects.
lt – Optional[Any] – Check if x < lt.
le – Optional[Any] – Check if x <= le.
ne – Optional[Any] – Check if x != ne.
eq – Optional[Any] – Check if x == eq.
ge – Optional[Any] – Check if x >= ge.
gt – Optional[Any] – Check if x > gt.
- Example
from argscheck import check, Number # Check if a number between 0 (inclusive) and 10 (exclusive) checker = Number(ge=0, lt=10) check(checker, 0) # Passes, 0 is returned check(checker, 5.0) # Passes, 5 is returned check(checker, 10) # Fails, a ValueError is raised check(checker, 'a') # Fails, a TypeError is raised
- Example
from argscheck import check, Number # Check if a number between 0 (inclusive) and 25 (exclusive) but not equal to 14 checker = (0.0 <= (Number < 25)) != 14 check(checker, 0) # Passes, 0 is returned check(checker, 11.0) # Passes, 5 is returned check(checker, 26) # Fails, a ValueError is raised check(checker, 14) # Fails, a ValueError is raised
- class argscheck.numeric.NonNegativeNumber(*args, **kwargs)¶
Same as
Number, plus, x >= 0 must be True.
- class argscheck.numeric.NonNegativeFloat(*args, **kwargs)¶
Same as
Float, plus, x >= 0 must be True.
- class argscheck.numeric.NonPositiveNumber(*args, **kwargs)¶
Same as
Number, plus, x <= 0 must be True.
- class argscheck.numeric.NonPositiveFloat(*args, **kwargs)¶
Same as
Float, plus, x <= 0 must be True.
- class argscheck.numeric.Sized(*args, len_lt=None, len_le=None, len_ne=None, len_eq=None, len_ge=None, len_gt=None, **kwargs)¶
Check the length of x, as returned by len(x).
- Parameters
len_lt – Optional[int] – Check if len(x) < len_lt.
len_le – Optional[int] – Check if len(x) <= len_le.
len_ne – Optional[int] – Check if len(x) != len_ne.
len_eq – Optional[int] – Check if len(x) == len_eq.
len_ge – Optional[int] – Check if len(x) >= len_ge.
len_gt – Optional[int] – Check if len(x) > len_gt.
- Example
from argscheck import check, Sized # Check if length is equal to 3 check(Sized(len_eq=3), ['a', 'b', 'c']) # Passes, returns ['a', 'b', 'c'] check(Sized(len_eq=3), 'abc') # Passes, returns 'abc' check(Sized(len_eq=3), {'a', 'b'}) # Fails, raises ValueError (length is 2 instead of 3) check(Sized(len_eq=3), 123) # Fails, raises TypeError (int does not have a length)
- class argscheck.numeric.NonEmpty(*args, **kwargs)¶
Check if the length of x is greater than zero.
- Example
from argscheck import check, NonEmpty # Check if non empty check(NonEmpty, ['a', 'b', 'c']) # Passes, returns ['a', 'b', 'c'] check(NonEmpty, 'abc') # Passes, returns 'abc' check(NonEmpty, '') # Fails, raises ValueError (empty string) check(NonEmpty, []) # Fails, raises ValueError (empty list)