Advertisement
Guest User

AppendingListDictNest

a guest
Jul 30th, 2014
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. $> cat input.txt
  2. {'d2': [{'childs': [{'childs': [{'childs': None, 'name': 'X'}], 'name': 'C'}], 'name': 'B'}], 'd1': [{'childs': [{'childs': [{'childs': [{'childs': None, 'name': 'D'}], 'name': 'C'}], 'name': 'B'}], 'name': 'A'}, {'childs': None, 'name': 'E'}]}
  3.  
  4. $>vi test1.py
  5. class Node(object):
  6.     def __init__(self, name):
  7.         self.name = name
  8.         self.children = []
  9.  
  10.     def add_child(self, child):
  11.         self.children.append(child)
  12.  
  13.     def flatten_to_dict(self):
  14.         my_dict = {'name': self.name, 'childs': []}
  15.  
  16.         for child in self.children:
  17.             my_dict['childs'].append(child.flatten_to_dict())
  18.  
  19.         return my_dict
  20.  
  21.     @staticmethod
  22.     def from_dict(inp_dict):
  23.         root = Node(inp_dict['name'])
  24.         if not inp_dict['childs']:
  25.             return root
  26.  
  27.         for child in inp_dict['childs']:
  28.             root.add_child(Node.from_dict(child))
  29.         return root
  30.  
  31.     def __repr__(self):
  32.         return "%s" % self.flatten_to_dict()
  33.  
  34.     def find_node_with_name(self, name):
  35.         if self.name == name:
  36.             return self
  37.  
  38.         for child in self.children:
  39.             found_node = child.find_node_with_name(name)
  40.             if found_node:
  41.                 return found_node
  42.         return None
  43.  
  44.     @staticmethod
  45.     def merge_nodes(node1, node2):
  46.         the_node = node1.find_node_with_name(node2.name)
  47.  
  48.         if not the_node:
  49.             return None
  50.  
  51.         for child in node2.children:
  52.             n = Node.merge_nodes(the_node, child)
  53.             if not n:
  54.                 the_node.add_child(child)
  55.         return the_node
  56.  
  57.     def merge_with_node(self, another_node):
  58.         # check if this node's name is present in the node or in its children
  59.         node_to_return = Node.from_dict(self.flatten_to_dict())
  60.         x = Node.merge_nodes(node_to_return, another_node)
  61.         return node_to_return
  62.  
  63.  
  64. if __name__ == '__main__':
  65.     data = eval(open('input.txt').read())
  66.     d1 = data['d1']
  67.     d2 = data['d2']
  68.     root_1 = Node.from_dict(d1[0])
  69.  
  70.     root_2 = Node.from_dict(d2[0])
  71.  
  72.     Dict1 = map(Node.from_dict, d1)
  73.     Dict2 = map(Node.from_dict, d2)
  74.  
  75.     # assuming Dict2 should just be one element, then Dict2[0]
  76.     Dict2 = Dict2[0]
  77.     result = []
  78.     # try to merge with every dict in Dict1
  79.     for _dict in Dict1:
  80.         res = _dict.merge_with_node(Dict2)
  81.         result.append(res)
  82.  
  83.     print result
  84.  
  85.  
  86. $>python test1.py
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement