Skip to content

Instantly share code, notes, and snippets.

@derrickturk
Last active May 14, 2026 14:46
Show Gist options
  • Select an option

  • Save derrickturk/4045005766826874ac2a667b34930165 to your computer and use it in GitHub Desktop.

Select an option

Save derrickturk/4045005766826874ac2a667b34930165 to your computer and use it in GitHub Desktop.
pretty-printer for twoninetyex tries
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