character_range.interval#

Implementation of Interval, CharacterInterval and ByteInterval.

class CharacterInterval(start: _Char, end: _Char)[source]#

Class variables

_max_value: ClassVar[int] = 1114111#

The maximum integral value of a Unicode codepoint: 0x10FFFF.

class ByteInterval(start: _Char, end: _Char)[source]#

Class variables

_max_value: ClassVar[int] = 255#

The maximum integral value of a byte (16 bits): 0xFF.

class Interval(start: _Char, end: _Char)[source]#

An interval (both ends inclusive) of characters, represented using either str or bytes.

Class variables

_max_value: ClassVar[int]#

The maximum integral value that can be converted to a character.

Magic methods

__hash__() int[source]#

Return hash(self).

__iter__() Iterator[_Char][source]#

Lazily yield each character or byte.

__reversed__() Iterator[_Char][source]#

Lazily yield each character or byte in reverse order.

__getitem__(item: slice) Self[source]#
__getitem__(item: SupportsIndex) _Char

O(1) indexing of character or byte. slice objects are also supported.

__len__() int[source]#

The length of the interval, equivalent to codepoint(end) - codepoint(start) + 1.

__contains__(item: Any) bool[source]#

Assert that item is a valid element and that it is lexicographically greater than or equals to that of start and less than or equals to that of end.

__repr__() str[source]#

Return repr(self).

__str__() str[source]#

Return an ASCII representation of the range, typically looks like \x00-a, \--\uFFFD or \U00100000.

__eq__(other: object) bool[source]#

Two intervals are equal if one is an instance of the other’s class and their endpoints have the same integral values.

__and__(other: Self) bool[source]#

See intersects().

Properties

property start: _Char#

The starting endpoint of the interval.

property end: _Char#

The ending endpoint of the interval.

abstract property element_type: type[_Char]#

A class-based property that returns the type of the interval’s elements.

Methods

to_codepoint_range() range[source]#

Convert the interval to a native range that would yield the codepoints of the elements of the interval.

intersects(other: Self) bool[source]#

Whether two intervals intersect each other.

Class methods

classmethod from_codepoint_range(codepoint_range: range, /) Self[source]#

Construct an interval from a range of codepoints.

As a technical limit, for a CharacterInterval, the codepoint of an endpoint must not be negative or greater than 0x10FFFF. Similarly, for a ByteInterval, the integral value of an endpoint must be in the interval [0, 255].

exception InvalidIntervalDirection(start: _Char, stop: _Char)[source]#

Bases: ValueError

Raised when an interval constructor is passed a start whose value is greater than that of end.

exception NotACharacter(actual: object)[source]#

Bases: ValueError

Raised when an object is expected to be a character (a str of length 1) but it is not one.

exception NotAByte(actual: object)[source]#

Bases: ValueError

Raised when an object is expected to be a byte (a bytes object of length 1) but it is not one.

exception InvalidCodepointRange[source]#

Bases: ValueError

Raised when a range cannot be interpreted as an Interval’s codepoint range.