String Types¶
IsAnyStr ¶
IsAnyStr(*, min_length: Optional[int] = None, max_length: Optional[int] = None, case: Literal['upper', 'lower', None] = None, regex: Union[None, T, Pattern[T]] = None, regex_flags: int = 0)
Bases: DirtyEquals[T]
Comparison of str
or bytes
objects.
This class allow comparison with both str
and bytes
but is subclassed
by IsStr
and IsBytes
which restrict comparison to
str
or bytes
respectively.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
min_length |
Optional[int]
|
minimum length of the string/bytes |
None
|
max_length |
Optional[int]
|
maximum length of the string/bytes |
None
|
case |
Literal['upper', 'lower', None]
|
check case of the string/bytes |
None
|
regex |
Union[None, T, Pattern[T]]
|
regular expression to match the string/bytes with, |
None
|
regex_flags |
int
|
optional flags for the regular expression |
0
|
Examples:
from dirty_equals import IsAnyStr
assert 'foobar' == IsAnyStr()
assert b'foobar' == IsAnyStr()
assert 123 != IsAnyStr()
assert 'foobar' == IsAnyStr(regex='foo...')
assert 'foobar' == IsAnyStr(regex=b'foo...')
assert 'foobar' == IsAnyStr(min_length=6)
assert 'foobar' != IsAnyStr(min_length=8)
assert 'foobar' == IsAnyStr(case='lower')
assert 'Foobar' != IsAnyStr(case='lower')
IsStr ¶
IsStr(*, min_length: Optional[int] = None, max_length: Optional[int] = None, case: Literal['upper', 'lower', None] = None, regex: Union[None, T, Pattern[T]] = None, regex_flags: int = 0)
Checks if the value is a string, and optionally meets some constraints.
IsStr
is a subclass of IsAnyStr
and therefore allows all the same arguments.
Examples:
from dirty_equals import IsStr
assert 'foobar' == IsStr()
assert b'foobar' != IsStr()
assert 'foobar' == IsStr(regex='foo...')
assert 'FOOBAR' == IsStr(min_length=5, max_length=10, case='upper')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
min_length |
Optional[int]
|
minimum length of the string/bytes |
None
|
max_length |
Optional[int]
|
maximum length of the string/bytes |
None
|
case |
Literal['upper', 'lower', None]
|
check case of the string/bytes |
None
|
regex |
Union[None, T, Pattern[T]]
|
regular expression to match the string/bytes with, |
None
|
regex_flags |
int
|
optional flags for the regular expression |
0
|
Examples:
from dirty_equals import IsAnyStr
assert 'foobar' == IsAnyStr()
assert b'foobar' == IsAnyStr()
assert 123 != IsAnyStr()
assert 'foobar' == IsAnyStr(regex='foo...')
assert 'foobar' == IsAnyStr(regex=b'foo...')
assert 'foobar' == IsAnyStr(min_length=6)
assert 'foobar' != IsAnyStr(min_length=8)
assert 'foobar' == IsAnyStr(case='lower')
assert 'Foobar' != IsAnyStr(case='lower')
IsBytes ¶
IsBytes(*, min_length: Optional[int] = None, max_length: Optional[int] = None, case: Literal['upper', 'lower', None] = None, regex: Union[None, T, Pattern[T]] = None, regex_flags: int = 0)
Checks if the value is a bytes object, and optionally meets some constraints.
IsBytes
is a subclass of IsAnyStr
and therefore allows all the same arguments.
Examples:
from dirty_equals import IsBytes
assert b'foobar' == IsBytes()
assert 'foobar' != IsBytes()
assert b'foobar' == IsBytes(regex=b'foo...')
assert b'FOOBAR' == IsBytes(min_length=5, max_length=10, case='upper')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
min_length |
Optional[int]
|
minimum length of the string/bytes |
None
|
max_length |
Optional[int]
|
maximum length of the string/bytes |
None
|
case |
Literal['upper', 'lower', None]
|
check case of the string/bytes |
None
|
regex |
Union[None, T, Pattern[T]]
|
regular expression to match the string/bytes with, |
None
|
regex_flags |
int
|
optional flags for the regular expression |
0
|
Examples:
from dirty_equals import IsAnyStr
assert 'foobar' == IsAnyStr()
assert b'foobar' == IsAnyStr()
assert 123 != IsAnyStr()
assert 'foobar' == IsAnyStr(regex='foo...')
assert 'foobar' == IsAnyStr(regex=b'foo...')
assert 'foobar' == IsAnyStr(min_length=6)
assert 'foobar' != IsAnyStr(min_length=8)
assert 'foobar' == IsAnyStr(case='lower')
assert 'Foobar' != IsAnyStr(case='lower')