jsonschema_extras.formats.slice_string module¶
- jsonschema_extras.formats.slice_string.SLICE_STRING_PATTERN = re.compile('^(?P<start>(?:\\-)?\\d+)?:(?P<stop>(?:\\-)?\\d+)?(?::(?P<step>(?:\\-)?\\d+))?$')¶
Compiled regex pattern for a slice string.
Matches Python’s
slicesyntax in string form:start:stop:stepwhere all components are optional integers (may be negative).
- jsonschema_extras.formats.slice_string.is_slice_string(instance)[source]¶
Tests if a string specifies a slice.
A slice string follows Python’s
slicesyntax:start:stop:stepwherestart,stop, andstepare optional integers that may be negative. At minimum, the string must contain at least one colon.- Return type:
- Returns:
Whether the string is a slice string (by the Python’s
slicesyntax).- Raises:
TypeError – If the given object is not a string.
- Parameters:
instance (object)
Examples
Valid slice strings:
>>> is_slice_string('1:5') True >>> is_slice_string('::2') True >>> is_slice_string('-5:') True >>> is_slice_string(':') True
Invalid slice strings:
>>> is_slice_string('1.5:10') False >>> is_slice_string('a:b') False
Type checking:
>>> is_slice_string(123) Traceback (most recent call last): ... TypeError: str expected, got <class 'int'>
- jsonschema_extras.formats.slice_string.is_slice_string_info = ('slice-string', <function is_slice_string>, (<class 'TypeError'>, <class 'ValueError'>))¶
Format
slice-stringchecked byis_slice_string_info()