i build tree structure can constructed easily, without having remember exact name of branch. know question has been discussed here, solution works fine, still requires exact name of key must remembered. following solution can use autocompletion:
class tree_dot: """ class create tree structure """ def __init__(self, name): self.name = name self.branches = [] def add_branch(self,branch_name): """ method add branch tree tree object """ = tree_dot(branch_name) setattr(self, branch_name, a) self.branches.append(a) the output looking this:
>>> tree = tree_dot('tree') >>> tree.add_branch('first') >>> tree.first.add_branch('second') >>> tree.first.second.add_branch('third') >>> tree.first.second.third.name = 'this third branch !' however, feel uncomfortable such long name.name.name.name. pose problem? there better way it?
since is recursive, extent impossible avoid many dots. in terms of using classes represent recursive results, normal.
if want avoid remembering key names, add default attribute list of connections:
tree.c[0].c[0].c[1] , or whatever indexes exist.
an alternate way have keys/value system. is:
(['first','second','third'], tree_dot()) which way remember keys. if needing application network problems (e.g. travelling salesman), can useful, can stored in flat array (making comparisons , other functions easier) without losing data.
Comments
Post a Comment