projects
/
python_utils.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Code cleanup for run_test.py
[python_utils.git]
/
collect
/
trie.py
diff --git
a/collect/trie.py
b/collect/trie.py
index 70d57b1338ca8e19e4bde07e33e440a846bdc79e..547d67a4124abc07f425ecf21616bf2c509beb4e 100644
(file)
--- a/
collect/trie.py
+++ b/
collect/trie.py
@@
-1,6
+1,15
@@
#!/usr/bin/env python3
#!/usr/bin/env python3
-from typing import Any, Sequence
+# © Copyright 2021-2022, Scott Gasch
+
+"""This is a Trie class, see: https://en.wikipedia.org/wiki/Trie.
+
+It attempts to follow Pythonic container patterns. See doctests
+for examples.
+
+"""
+
+from typing import Any, Generator, Sequence
class Trie(object):
class Trie(object):
@@
-17,6
+26,7
@@
class Trie(object):
self.end = "~END~"
self.length = 0
self.viz = ''
self.end = "~END~"
self.length = 0
self.viz = ''
+ self.content_generator: Generator[str] = None
def insert(self, item: Sequence[Any]):
"""
def insert(self, item: Sequence[Any]):
"""
@@
-246,12
+256,11
@@
class Trie(object):
self,
padding: str,
pointer: str,
self,
padding: str,
pointer: str,
- parent: str,
node: Any,
has_sibling: bool,
):
if node is None:
node: Any,
has_sibling: bool,
):
if node is None:
- return
+ return
''
if node is not self.root:
ret = f'\n{padding}{pointer}'
if has_sibling:
if node is not self.root:
ret = f'\n{padding}{pointer}'
if has_sibling:
@@
-276,7
+285,7
@@
class Trie(object):
has_sibling = False
pointer += f'{child}'
child_count -= 1
has_sibling = False
pointer += f'{child}'
child_count -= 1
- ret += self.repr_fancy(padding, pointer, node
, node
[child], has_sibling)
+ ret += self.repr_fancy(padding, pointer, node[child], has_sibling)
return ret
def repr_brief(self, node, delimiter):
return ret
def repr_brief(self, node, delimiter):
@@
-289,7
+298,7
@@
class Trie(object):
>>> t.insert([10, 10, 10, 1])
>>> t.insert([10, 10, 10, 2])
>>> t.repr_brief(t.root, '.')
>>> t.insert([10, 10, 10, 1])
>>> t.insert([10, 10, 10, 2])
>>> t.repr_brief(t.root, '.')
- '10.[0.0.[1,
2], 10.10.[1,
2]]'
+ '10.[0.0.[1,
2],10.10.[1,
2]]'
"""
child_count = 0
"""
child_count = 0
@@
-299,11
+308,11
@@
class Trie(object):
child_count += 1
child_rep = self.repr_brief(node[child], delimiter)
if len(child_rep) > 0:
child_count += 1
child_rep = self.repr_brief(node[child], delimiter)
if len(child_rep) > 0:
- my_rep += str(child) + delimiter + child_rep + ",
"
+ my_rep += str(child) + delimiter + child_rep + ","
else:
else:
- my_rep += str(child) + ",
"
+ my_rep += str(child) + ","
if len(my_rep) > 1:
if len(my_rep) > 1:
- my_rep = my_rep[:-
2
]
+ my_rep = my_rep[:-
1
]
if child_count > 1:
my_rep = f'[{my_rep}]'
return my_rep
if child_count > 1:
my_rep = f'[{my_rep}]'
return my_rep
@@
-331,7
+340,7
@@
class Trie(object):
└──2
"""
└──2
"""
- return self.repr_fancy('', '*', self.root,
self.root,
False)
+ return self.repr_fancy('', '*', self.root, False)
if __name__ == '__main__':
if __name__ == '__main__':