Sequences

This module contains checkers for sequence objects.

In this context, a sequence is a class that:

  1. Has __len__() implemented.

  2. Has __getitem__() implemented, which accepts integers starting from zero and up to (and not including) the object’s own length.

  3. Can be instantiated from an iterable.

Moreover, a mutable sequence is a class that:

  1. Has the properties of a sequence.

  2. Has __setitem__() implemented, which accepts the same keys as __getitem__().

Sequences (mutable or otherwise) can be homogeneous, i.e. all items in it have some shared properties. Homogeneity can be checked using the *args parameter:

If a non-empty *args is provided, then, the resulting checker is applied to each item in the sequence.

Checkers that convert values (like Optional for example) are applied as follows:

  1. For mutable sequences, converted items are set inplace.

  2. For (non mutable) sequences, a new sequence instance is created from the converted items (this will happen only if actual conversion took place for at least one item).

class argscheck.sequence.Sequence(*args, **kwargs)

Check if x is a sequence.

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 Sequence

# Check if a set of length at least 2
checker = Sequence(str, len_ge=2)

checker.check(['a', 'b'])    # Passes, returns ['a', 'b']
checker.check({'a', 'b'})    # Fails, raises TypeError (set is not a sequence)
checker.check(['a'])         # Fails, raises ValueError (length is less than 2)
checker.check(['a', 1])      # Fails, raises TypeError (not all items are str)
class argscheck.sequence.NonEmptySequence(*args, **kwargs)

Same as Sequence, plus, the length of x must be greater than zero.

class argscheck.sequence.Tuple(*args, **kwargs)

Same as Sequence, plus, x must be a tuple.

class argscheck.sequence.NonEmptyTuple(*args, **kwargs)

Same as Tuple, plus, the length of x must be greater than zero.

class argscheck.sequence.MutableSequence(*args, **kwargs)

Check if x is a mutable sequence.

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.

class argscheck.sequence.NonEmptyMutableSequence(*args, **kwargs)

Same as MutableSequence, plus, the length of x must be greater than zero.

class argscheck.sequence.List(*args, **kwargs)

Same as MutableSequence, plus, x must be a list.

class argscheck.sequence.NonEmptyList(*args, **kwargs)

Same as List, plus, the length of x must be greater than zero.