From 87f41f2e8f18fa2eafb358868eef75549acb866f Mon Sep 17 00:00:00 2001 From: manthanchauhan Date: Fri, 24 May 2019 18:02:41 +0530 Subject: [PATCH 1/2] implemented HTML based graphic view for CharTrie. --- pygtrie.py | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/pygtrie.py b/pygtrie.py index 0c41e05..4b93839 100644 --- a/pygtrie.py +++ b/pygtrie.py @@ -1185,6 +1185,114 @@ class CharTrie(Trie): def _key_from_path(self, path): return ''.join(path) + def _dfs(self, node=None, level=0): + """ + Traverses the Trie object in a depth first manner, required by the :function: + `CharTrie.graphic_view()`. + :parameter node: The trie node from which the traversal should begin (exclusive). + :parameter level: Distance of the current node from root node, required in + generating HTML list structure. + :return: None + """ + + # default node is the root of the CharTrie + if node is None: + node = self._root + + """:function: `CharTrie._dfs()` is a recursive function, the provided node is + traversed in the previous iteration. Therefore, if the node has no children, the + function call ends. + """ + if len(node.children.keys()) == 0: + return + + for key, value in node.children.items(): + + # if the node path is a valid key, yield the value to be displayed + # in the graphic view + if value.value != _SENTINEL: + yield (key, level, value.value) + + else: + yield (key, level) + + # traverse children of the current node before traversing the sibligs + # in depth first manner + yield from self._dfs(node=value, level=level+1) + + return + + def graphic_view(self): + """creates an HTML view of the CharTrie object in a file named, + chartrie_graphic_view.html + + It can be used during debugging or in applications like competitive + programming where close interaction with data is required. + + Uses DFS traversal result and converts it into a graphical view using + HTML lists. + """ + dfs_sequence = [data_level_tuple for data_level_tuple in self._dfs()] + + # the HTML list structure of the CharTrie object is created in the file + # 'chartrie_graphic_view.html' + + with open('chartrie_graphic_view.html', 'w') as html_file: + html_file.write('') + class StringTrie(Trie): """:class:`pygtrie.Trie` variant accepting strings with a separator as keys. From a90093f7ab543ea01ba2f351c8286fadd786ed45 Mon Sep 17 00:00:00 2001 From: manthanchauhan913 Date: Thu, 31 Oct 2019 21:48:47 +0530 Subject: [PATCH 2/2] implemented HTML based graphic view for CharTrie. --- example.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/example.py b/example.py index 3f647ce..c126996 100755 --- a/example.py +++ b/example.py @@ -1,6 +1,7 @@ #!/usr/bin/python -"""trie module example code.""" +"""trie module example code. +""" __author__ = 'Michal Nazarewicz ' __copyright__ = 'Copyright 2014 Google Inc.'