Last active
May 14, 2026 14:46
-
-
Save derrickturk/4045005766826874ac2a667b34930165 to your computer and use it in GitHub Desktop.
pretty-printer for twoninetyex tries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from example.twoninetyex import SPEC297_TRIE | |
| def pprint(t, lvl=0, remaining_siblings=()): | |
| indent = lvl * [' ', ' '] | |
| for i in range(lvl): | |
| if remaining_siblings[i] > 1: | |
| indent[i * 2] = '┃' | |
| if i == lvl - 1: | |
| if remaining_siblings[i] == 1: | |
| indent[i * 2] = '┗' | |
| indent[i * 2 + 1] = '━' | |
| else: | |
| indent[i * 2] = '┣' | |
| indent[i * 2 + 1] = '━' | |
| if t.record: | |
| r = t.record[0] | |
| lbl = ': ' + r['description'] | |
| else: | |
| lbl = '' | |
| if t.character == '': | |
| char = '<START>' | |
| elif t.character == ' ': | |
| char = '<END>' | |
| else: | |
| char = t.character | |
| print(f"{''.join(indent)}{char}{lbl}") | |
| for i, s in enumerate(t.suffixes.values()): | |
| pprint(s, lvl+1, (*remaining_siblings, len(t.suffixes) - i)) | |
| if __name__ == '__main__': | |
| pprint(SPEC297_TRIE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment