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 slice syntax in string form: start:stop:step where 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 slice syntax: start:stop:step where start, stop, and step are optional integers that may be negative. At minimum, the string must contain at least one colon.

Return type:

bool

Returns:

Whether the string is a slice string (by the Python’s slice syntax).

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-string checked by is_slice_string_info()