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_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.

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.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 by 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 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)