Skip to content

CharacterMapping

This module maps Unicode characters to native encoding.

Source characters can be specified as Unicode characters ("<character>" or '<character>'), as decimal values (<number>), or as hexadecimal values (0x<number>).

Destination characters are specified as integer values.

Mapping specifications can be given as a list or a string:

  • A list specification has the following formats:

    • For single character mapping: [<source>, <target>]

    • For range mapping: [<source_start>, <source_end>, <target_start>]

  • A string specification has the following format:

    • For single character mapping: <source>:<target>

    • For range mapping: <source_start>-<source_end>:<target_start>

CharacterMapping(mappings=[])

Map Unicode characters to native encoding.

PARAMETER DESCRIPTION
mappings

List of mappings to add.

TYPE: list[list[int | str] | str] DEFAULT: []

add_mapping(arguments)

Add a mapping.

PARAMETER DESCRIPTION
arguments

Mapping to add.

TYPE: list[int | str] | str

RAISES DESCRIPTION
ValueError

If mapping specification is invalid.

add_range(source_start, source_end, target)

Add a range of mappings.

PARAMETER DESCRIPTION
source_start

Start of Unicode character range.

TYPE: int | str

source_end

End of Unicode character range.

TYPE: int | str

target

Start of native character range.

TYPE: int

add_single(character, target)

Add a single mapping.

PARAMETER DESCRIPTION
character

Unicode character.

TYPE: int | str

target

Native character.

TYPE: int

encode(string)

Encode Unicode string.

PARAMETER DESCRIPTION
string

String to encode.

TYPE: str

RETURNS DESCRIPTION
bytes

Encoded string.

RAISES DESCRIPTION
ValueError

If string contains unmapped characters.

petscii() staticmethod

Create PETSCII character mapping.

RETURNS DESCRIPTION
CharacterMapping

PETSCII character mapping.