Numeric

This module contains 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: {< | <= | != | == | >= | >}.

Parameters
  • other_typeOptional[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.

  • ltOptional[Any] – Check if x < lt.

  • leOptional[Any] – Check if x <= le.

  • neOptional[Any] – Check if x != ne.

  • eqOptional[Any] – Check if x == eq.

  • geOptional[Any] – Check if x >= ge.

  • gtOptional[Any] – Check if x > gt.

class argscheck.numeric.Int(*args, **kwargs)

Same as Number, plus, x must be an int.

class argscheck.numeric.Float(*args, **kwargs)

Same as Number, plus, x must be a float.

class argscheck.numeric.PositiveNumber(*args, **kwargs)

Same as Number, plus, x > 0 must be True.

class argscheck.numeric.PositiveInt(*args, **kwargs)

Same as Int, plus, x > 0 must be True.

class argscheck.numeric.PositiveFloat(*args, **kwargs)

Same as Float, plus, x > 0 must be True.

class argscheck.numeric.NonNegativeNumber(*args, **kwargs)

Same as Number, plus, x >= 0 must be True.

class argscheck.numeric.NonNegativeInt(*args, **kwargs)

Same as Int, plus, x >= 0 must be True.

class argscheck.numeric.NonNegativeFloat(*args, **kwargs)

Same as Float, plus, x >= 0 must be True.

class argscheck.numeric.NegativeNumber(*args, **kwargs)

Same as Number, plus, x < 0 must be True.

class argscheck.numeric.NegativeInt(*args, **kwargs)

Same as Int, plus, x < 0 must be True.

class argscheck.numeric.NegativeFloat(*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.NonPositiveInt(*args, **kwargs)

Same as Int, 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 from len(x)).

Parameters
  • len_ltOptional[int] – Check if len(x) < len_lt.

  • len_leOptional[int] – Check if len(x) <= len_le.

  • len_neOptional[int] – Check if len(x) != len_ne.

  • len_eqOptional[int] – Check if len(x) == len_eq.

  • len_geOptional[int] – Check if len(x) >= len_ge.

  • len_gtOptional[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 x is 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)