Sequences¶
This module contains checkers for sequence objects.
In this context, a sequence is a class that:
Has
__len__()implemented.Has
__getitem__()implemented, which accepts integers starting from zero and up to (and not including) the object’s own length.Can be instantiated from an iterable.
Moreover, a mutable sequence is a class that:
Has the properties of a sequence.
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:
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
xis 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 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 ofxmust be greater than zero.
- class argscheck.sequence.NonEmptyTuple(*args, **kwargs)¶
Same as
Tuple, plus, the length ofxmust be greater than zero.
- class argscheck.sequence.MutableSequence(*args, **kwargs)¶
Check if
xis 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 ofxmust be greater than zero.
- class argscheck.sequence.List(*args, **kwargs)¶
Same as
MutableSequence, plus,xmust be alist.