character_range.maps

Implementation of IndexMap, CharacterMap and ByteMap.

class IndexMap(intervals: Iterable[Interval[_Char]], lookup_char: Callable[[_Char], int] | None = None, lookup_index: Callable[[int], _Char] | None = None)[source]

A two-way mapping between character or byte to its corresponding index.

Methods

__hash__() int[source]

Return hash(self).

__len__() int[source]
__getitem__(item: _Char) int[source]
__getitem__(item: int) _Char

Either look for the character/index in the underlying maps, or delegate that task to the look-up functions given.

Results are cached.

Raises:
  • ValueError – If item is neither a character/byte nor an index.

  • IndexError – If lookup_char

__contains__(item: object) bool[source]

Properties

property intervals: tuple[Interval[_Char], ...]

The intervals that make up the map.

property element_type: type[_Char]

The type of the map’s elements.

class CharacterMap(intervals: Iterable[Interval[_Char]], lookup_char: Callable[[_Char], int] | None = None, lookup_index: Callable[[int], _Char] | None = None)[source]
ASCII_LOWERCASE: ClassVar[CharacterMap] = CharacterMap(a-z)
ASCII_UPPERCASE: ClassVar[CharacterMap] = CharacterMap(A-Z)
ASCII_LETTERS: ClassVar[CharacterMap] = CharacterMap(a-zA-Z)
ASCII_DIGITS: ClassVar[CharacterMap] = CharacterMap(0-9)
LOWERCASE_HEX_DIGITS: ClassVar[CharacterMap] = CharacterMap(0-9a-f)
UPPERCASE_HEX_DIGITS: ClassVar[CharacterMap] = CharacterMap(0-9A-F)
LOWERCASE_BASE_36: ClassVar[CharacterMap] = CharacterMap(0-9a-z)
UPPERCASE_BASE_36: ClassVar[CharacterMap] = CharacterMap(0-9A-Z)
ASCII: ClassVar[CharacterMap] = CharacterMap(\x00-\xFF)
NON_ASCII: ClassVar[CharacterMap] = CharacterMap(\u0100-\U0010FFFF)
UNICODE: ClassVar[CharacterMap] = CharacterMap(\x00-\U0010FFFF)
class ByteMap(intervals: Iterable[Interval[_Char]], lookup_char: Callable[[_Char], int] | None = None, lookup_index: Callable[[int], _Char] | None = None)[source]
ASCII_LOWERCASE: ClassVar[ByteMap] = ByteMap(a-z)
ASCII_UPPERCASE: ClassVar[ByteMap] = ByteMap(A-Z)
ASCII_LETTERS: ClassVar[ByteMap] = ByteMap(a-zA-Z)
ASCII_DIGITS: ClassVar[ByteMap] = ByteMap(0-9)
LOWERCASE_HEX_DIGITS: ClassVar[ByteMap] = ByteMap(0-9a-f)
UPPERCASE_HEX_DIGITS: ClassVar[ByteMap] = ByteMap(0-9A-F)
LOWERCASE_BASE_36: ClassVar[ByteMap] = ByteMap(0-9a-z)
UPPERCASE_BASE_36: ClassVar[ByteMap] = ByteMap(0-9A-Z)
ASCII: ClassVar[ByteMap] = ByteMap(\x00-\xFF)
exception NoIntervals[source]

Bases: ValueError

Raised when no intervals are passed to the map constructor.

exception OverlappingIntervals[source]

Bases: ValueError

Raised when there are at least two overlapping intervals in the list of intervals passed to the map constructor.

exception ConfigurationConflict[source]

Bases: ValueError

Raised when the map constructor is passed:

  • A list of intervals whose elements don’t have the same type.

  • Only one lookup function but not the other.

exception InvalidIndex(length: int, actual_index: object)[source]

Bases: LookupError

Raised when the index returned by a lookup_char function is not a valid index.

exception InvalidChar(actual_char: object, expected_type: type[str] | type[bytes])[source]

Bases: LookupError

Raised when the character returned by a lookup_index function is not of the type expected by the map.