MCPcopy Index your code
hub / github.com/rspeer/python-ftfy / show_char_table

Function show_char_table

scripts/char_data_table.py:35–63  ·  view source on GitHub ↗
(chars: str, byte_min: int = 0, byte_max: int = 0xFF)

Source from the content-addressed store, hash-verified

33
34
35def show_char_table(chars: str, byte_min: int = 0, byte_max: int = 0xFF) -> None:
36 char_data: list[CharData] = []
37 for char in chars:
38 name = unicodedata.name(char, "<unknown>")
39 codept = ord(char)
40 encodings: list[tuple[str, int]] = []
41 for encoding in SAFE_ENCODINGS:
42 try:
43 encoded: bytes = char.encode(encoding)
44 byte: int = encoded[0]
45 encodings.append((encoding, byte))
46 except UnicodeEncodeError:
47 pass
48 if encodings:
49 char_data.append(CharData(name=name, codept=codept, encodings=encodings))
50 else:
51 print(f"No relevant encoding for {codept=}, {name=}")
52 char_data.sort(key=CharData.sort_key)
53 for cd in char_data:
54 encoding_info: list[str] = []
55 for encoding, byte in cd.encodings:
56 if byte_min <= byte <= byte_max:
57 info_str = f"{encoding}:{byte:X}"
58 encoding_info.append(info_str)
59 if encoding_info:
60 encoding_explanation = encoding_info[0]
61 else:
62 encoding_explanation = "???"
63 print(f' "\\N{{{cd.name}}}" # {encoding_explanation}')
64
65
66def run() -> None:

Callers 1

runFunction · 0.85

Calls 2

CharDataClass · 0.85
encodeMethod · 0.45

Tested by

no test coverage detected