python - Having users reference nodes of a tree structure -


i writing app stores hierarchical data in tree structure of python class instances. root node have list of children , each child have own children in list, , on.

users need operate directly on tree structure in easy way. easiest way being enumerated hierarchical list.

  1. lizards
  2. waterfowls
    1. goose

so example if user wanted add data goose in command line need work on item 2 sub item 1 or 2.1.

what's best way maintain these accessible references these tree node instances?

every node has timestamp , uuid. problem everytime program run must serialized , deserialized. sort timestamp bit unsure how come enumeration, if should dynamically done or stored in tree structure.

class node:      def __init__(self, name):         self.name = name         self.parent = none         self.children = []      def __repr__(self):         return self.name      def add_node(self, child):         child.set_parent(self)         self.add_child(child)      def get_node(self, pos):         try:             return self.children[pos]         except indexerror:             raise valueerror('node not exist')      def access(self, string):         node = self         in map(lambda i: int(i)-1, string.split('.')):             node = node.get_node(i)         return node      def set_parent(self, parent):         self.parent = parent      def add_child(self, child):         self.children.append(child)  >>> n = node('root') >>> l = node('lizards') >>> w = node('waterfowls') >>> g = node('goose') >>> n.add_node(l) >>> n.add_node(w) >>> w.add_node(g) >>> n.access('1') 'lizards' >>> n.access('2') 'waterfowls' >>> n.access('2.1') 'goose' >>> w.access('1') 'goose' 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -