Numeric¶
This module contains checkers for numeric arguments, as well as for sized arguments.
- class argscheck.numeric.Number(*args, **kwargs)¶
Check if
xis of a numeric type (intorfloat) and optionally, compares it to other value(s) using any of the following binary operators:{< | <= | != | == | >= | >}.- Parameters
other_type – Optional[Union[Type, Tuple[Type]]] – restricts the types to which
xcan be compared, e.g.other_type=intwithne=1.0will raise aTypeError. By default,xcan only be compared to otherintorfloatobjects.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.
- class argscheck.numeric.NonNegativeNumber(*args, **kwargs)¶
Same as
Number, plus,x >= 0must beTrue.
- class argscheck.numeric.NonNegativeFloat(*args, **kwargs)¶
Same as
Float, plus,x >= 0must beTrue.
- class argscheck.numeric.NonPositiveNumber(*args, **kwargs)¶
Same as
Number, plus,x <= 0must beTrue.
- class argscheck.numeric.NonPositiveFloat(*args, **kwargs)¶
Same as
Float, plus,x <= 0must beTrue.
- 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 fromlen(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 Sized # Check if length is equal to 3 checker = Sized(len_eq=3) checker.check(['a', 'b', 'c']) # Passes, returns ['a', 'b', 'c'] checker.check('abc') # Passes, returns 'abc' checker.check({'a', 'b'}) # Fails, raises ValueError (length is 2 instead of 3) checker.check(123) # Fails, raises TypeError (int does not have a length)
- class argscheck.numeric.NonEmpty(*args, **kwargs)¶
Check if length of
xis greater than zero.- Example
from argscheck import NonEmpty # Check if non empty checker = NonEmpty() checker.check(['a', 'b', 'c']) # Passes, returns ['a', 'b', 'c'] checker.check('abc') # Passes, returns 'abc' checker.check('') # Fails, raises ValueError (empty string) checker.check([]) # Fails, raises ValueError (empty list)