Finding elegant data presentation designs for a directory tree
I'm looking for advice on elegant designs for representing a directory of files without symbolic links in Python, where I can query relationships in terms of "owned" (for example, G is the / A / BC subdirectory). My current thinking goes in this direction:
For the root path, I os.path.walk () it from top to bottom. The two classes represent the node types I am interested in and I keep track of parent relationships with children.
class ADir(object):
def __init_(self, name, parent=None):
self.name = name
self.parent = parent
self.children = []
def add_child(self, id):
self.children.append(id)
class AFile(object):
def __init_(self, name, parent=None):
self.name = name
self.parent = parent
I would have to re-run checks for existing directories, functions that give me the directory / file location, etc. This is all starting to look very much like a re-implementation of existing generic tree algorithms.
Trawling Tray StackExchange, Google, etc. gives many different approaches. None of these I found used the natural boundaries given by the directory structure.
Any thoughts and pointers to discussions, blog posts, and code are appreciated.
source to share
The problem with tree structures in modern languages ββis that it is difficult to create one structure to fit all of these. There are many ways to create three (with or without parent pointer, children can be pairs (binary or red-black trees) or lists (with and without indexing search keys).
While it is possible to define traversal algorithms for all of them, each algorithm needs a separate implementation.
Then the problem of finding items in the tree arises. Are we working by index (pretty useless in binary trees)? Some kind of identifier? What type should an identifier have? How do we create paths from these IDs? How do we represent relative paths?
This is why we have maps and lists built into many modern languages, but without trees. As far as I know, Scala is one of the few OO languages ββthat supports the concept of a tree type, but only binary trees and even those that are somewhat odd.
Also, most OO languages ββdon't support enough ways to create classes from fragments of existing classes. You can inherit (but then you get everything), multiple inheritance (even more problems), mix in (some features of multiple inheritance without some disadvantages). But I'm really missing a function that says: take a method x()
from type Foo
and a method y()
from Bar
to build Baz
.
Without this, an OO tree-based base class needs a lot of customization for your specific use case, while directly executing the same function would require the same (or even less) lines of code.
source to share