Sequences¶
This page documents checkers for sequence objects.
We define a sequence as a class that:
Implements __len__().
Implements __getitem__(), which accepts (among others) integers starting from zero and up to (and not including) the object’s own length.
Can be instantiated from an iterable.
We define a mutable sequence as a class that:
Is a sequence.
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:
For mutable sequences, converted items are set inplace.
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_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 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.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_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.
- 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.