SHARE
TWEET

s

a guest May 22nd, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. DIVIDER = 2 * ' '
  3.  
  4. class Node():
  5.    
  6.     def __init__(self, name, parent=None):
  7.         self.nodes = []
  8.         self.name = name
  9.         self.parent = parent
  10.  
  11.     def add_node(self, node):
  12.         self.nodes.append(node)
  13.         node.change_parent(self)
  14.  
  15.     def get_nodes(self):
  16.         return self.nodes
  17.  
  18.     def change_parent(self, parent):
  19.         self.parent = parent   
  20.  
  21.  
  22.     def get_specfic_node(self, name):
  23.         for node in self.nodes:
  24.             if node.name == name:
  25.                 return node
  26.  
  27. def print_tree(root):
  28.     print(tree_getter(root)[:-1])
  29.  
  30. def write_to_file(filename):
  31.     string = tree_getter(root)
  32.     string = string[:-1]
  33.     print(string)
  34.  
  35.     with open(filename, 'w') as f:
  36.         f.write(string)
  37.  
  38.     print('done')
  39.  
  40.     return 0
  41.  
  42. def front_digit_count(string, other):
  43.     count = 0
  44.  
  45.     string_index = 0
  46.     other_index = 0
  47.  
  48.     while True:
  49.         if string[string_index] != other_index:
  50.             return count
  51.  
  52.         string_index += 1
  53.         other_index += 1   
  54.  
  55.         if string_index == len(string):
  56.             return count
  57.  
  58.         if other_index == len(other):
  59.             other_index = 0
  60.             count += 1
  61.  
  62. def read_from_file(filename):
  63.    
  64.     with open(filename, 'r') as f:
  65.         readlist = f.read()
  66.    
  67.     readlist = readlist.split('\n')
  68.  
  69.     cur_parents = []
  70.  
  71.     current = 1
  72.  
  73.     root = Node(readlist[0])
  74.  
  75.     cur_parents.append(root)
  76.  
  77.     while current != len(readlist):
  78.  
  79.         prev = readlist[current - 1]
  80.         cur = readlist[current]
  81.  
  82.         prev_indentation = front_digit_count(prev, DIVIDER)
  83.         cur_indentation = front_digit_count(cur, DIVIDER)
  84.  
  85.         prev_ind = len(DIVIDER) * prev_indentation
  86.         cur_ind  = len(DIVIDER) * cur_indentation
  87.  
  88.  
  89.         prev_name = prev[prev_ind:]
  90.         cur_name = cur[cur_ind:]
  91.  
  92.         if prev_indentation + 1 == cur_indentation:
  93.             cur_parents.append(Node(prev_name))
  94.         elif prev_indentation > cur_indentation:
  95.             diff = prev_indentation - cur_indentation
  96.             cur_parents = cur_parents[:-diff]
  97.        
  98.  
  99.         cur_parents[-1].add_node(Node(cur_name))
  100.  
  101.         current += 1
  102.  
  103.     return root
  104.  
  105. def tree_getter(root, indentation=0, stringy=''):
  106.  
  107.     stringy += (indentation * DIVIDER) + root.name + '\n'
  108.  
  109.     for node in root.get_nodes():
  110.         stringy += tree_getter(node, indentation+1)
  111.  
  112.     return stringy 
  113.  
  114. def add_nodes_to_root(root, nodes):
  115.     for node in nodes:
  116.         root.add_node(Node(node))
  117.  
  118. def traverse_one_direction(root, nodes):
  119.  
  120.     current = root
  121.  
  122.     for node in nodes:
  123.  
  124.         tempo = current.get_specfic_node(node)
  125.  
  126.         if tempo == None:
  127.             print('node not in there')
  128.                
  129.         else:
  130.             current = tempo
  131.  
  132.     return current
  133.  
  134.  
  135. if __name__ == '__main__':
  136.  
  137.     root = Node('start')
  138.     current_root = root
  139.  
  140.     while True:
  141.  
  142.         inp = input('\n')
  143.  
  144.         inp = inp.split(' ')
  145.  
  146.         arg, anp = inp[0], inp[1:]
  147.  
  148.  
  149.         if arg == '0':
  150.             print("current_root is " + current_root.name)
  151.        
  152.         elif arg == '1':
  153.             print_tree(root)
  154.  
  155.         elif arg == '2':
  156.             add_nodes_to_root(current_root, anp)
  157.  
  158.         elif arg == '3':
  159.  
  160.             current_root = traverse_one_direction(current_root, anp)
  161.  
  162.         elif arg == '4':
  163.  
  164.             if current_root == root:
  165.                 print('current_root is the main root')
  166.                 continue
  167.  
  168.             current_root = current_root.parent
  169.  
  170.         elif arg == '5':
  171.             write_to_file(anp[0])  
  172.        
  173.         elif arg == '6':
  174.             root = read_from_file(anp[0])
  175.             current_root = root
  176.  
  177.         elif arg == '7':
  178.             root =  Node('start')
  179.             current_root = root
  180.  
  181.         # add ability to remove nodes
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top