Skip to content

Instantly share code, notes, and snippets.

@pallabpain
Last active May 31, 2020 17:21
Show Gist options
  • Save pallabpain/827005098967a739d0eeb86ec03b416e to your computer and use it in GitHub Desktop.
Save pallabpain/827005098967a739d0eeb86ec03b416e to your computer and use it in GitHub Desktop.
Re-building directory tree from a list of random paths using pygtrie
import pygtrie
# 0 denotes a directory and 1 denotes a file
path_entries = [
("./pcache/pcache.egg-info/dependency_links.txt", 1),
("./pcache/LICENSE", 1),
("./pcache/build/lib/pcache/__init__.py", 1),
("./pcache/pcache.egg-info/top_level.txt", 1),
("./pcache/pcache/pcache.py", 1),
("./pcache/pcache/__init__.py", 1),
("./pcache", 0),
("./pcache/pcache.egg-info/PKG-INFO", 1),
("./pcache/README.md", 1),
("./pcache/setup.py", 1),
("./pcache/.travis.yml", 1),
("./pcache/pcache", 0),
("./pcache/build/lib", 0),
("./pcache/.gitignore", 1),
("./pcache/tests.py", 1),
("./pcache/dist/pcache-0.0.3-py3-none-any.whl", 1),
("./pcache/build/lib/pcache", 0),
("./pcache/build/lib/pcache/pcache.py", 1),
("./pcache/pcache.egg-info/SOURCES.txt", 1),
("./pcache/pcache.egg-info", 0),
("./pcache/build", 0),
("./pcache/build/bdist.macosx-10.9-x86_64", 0),
("./pcache/dist", 0),
("./pcache/dist/pcache-0.0.3.tar.gz", 1),
]
def callback(path_conv, path, children, is_file=False):
"""node_factory for the traverse method of the trie object
Read more about it here:
https://pygtrie.readthedocs.io/en/latest/#pygtrie.Trie.traverse
"""
if is_file:
print(path_conv(path))
return 0
if path:
print(path_conv(path))
return sum(children)
if __name__ == "__main__":
# Initialize the trie with the path separator
trie = pygtrie.StringTrie(separator="/")
# Add keys to the trie. The path forms the key and the entry type
# decides whether it is a leaf or not. It means that files will
# form the leaves whereas directories will ideally be the
# intermediate nodes
for key, value in path_entries:
trie[key] = bool(value)
# Finally, traverse the trie
trie.traverse(callback)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment