Sequences

This page documents checkers for sequence objects.

We define a sequence as a class that:

  1. Implements __len__().

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

  3. Can be instantiated from an iterable.

We define a mutable sequence as a class that:

  1. Is a sequence.

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

Sequences (mutable or otherwise) can be homogeneous, i.e. all items in it satisfy the same set of conditions.

Homogeneity can be checked for using by providing one or more positional arguments to the checker’s constructor.

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 check, Sequence


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

check(checker, ['a', 'b'])  # Passes, ['a', 'b'] is returned
check(checker, {'a', 'b'})  # Fails, a TypeError is raised (set is not a sequence)
check(checker, ['a'])       # Fails, a ValueError is raised (length is less than 2)
check(checker, ['a', 1])    # Fails, a TypeError is raised (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.