Skip to content

Commit b19c02f

Browse files
committed
comments and main for understanding
1 parent 902e74e commit b19c02f

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

08-def-type-hints/charindex.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,26 @@
2323
def tokenize(text: str) -> Iterator[str]: # <1>
2424
"""return iterable of uppercased words"""
2525
for match in RE_WORD.finditer(text):
26+
# for each word in phrase, group goes to string from re.Match object
2627
yield match.group().upper()
2728

2829
def name_index(start: int = 32, end: int = STOP_CODE) -> dict[str, set[str]]:
2930
index: dict[str, set[str]] = {} # <2>
3031
for char in (chr(i) for i in range(start, end)):
32+
# for each unicode character
3133
if name := unicodedata.name(char, ''): # <3>
34+
# lookup English word name of character
3235
for word in tokenize(name):
36+
# for each word in the name, add to dict with symbol in value set
3337
index.setdefault(word, set()).add(char)
3438
return index
3539
# end::CHARINDEX[]
40+
41+
if __name__ == '__main__':
42+
index = name_index(32, 65)
43+
print(f"""
44+
SIGN: { sorted(index['SIGN']) }
45+
DIGIT: { sorted(index['DIGIT']) }
46+
DIGIT & EIGHT: { index['DIGIT'] & index['EIGHT'] }
47+
# & is a set operation
48+
""")

0 commit comments

Comments
 (0)