Khadafi995

List dict

Apr 7th, 2017
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1.     def build_bloc(self):
  2.         '''
  3.        Build the principal bloc.
  4.        '''
  5.  
  6.         self._p_list = []
  7.         self._assossiated_list = {}
  8.        
  9.         tmp_list = list()
  10.         tag_class = defaultdict(dict)
  11.         tag_dict = defaultdict(dict)
  12.  
  13.         def get_astd(tag):
  14.             rgx = re.compile(r"xml-(\w+)")
  15.             if 'class' in tag.attrib:
  16.                 logging.info(tag.attrib['class'])
  17.                 try:
  18.                     logging.info('Checking if had many value')
  19.                     if tag.attrib['class'].split(' ')[1]:
  20.                         logging.info('Ok it has')
  21.                         for t in tag.attrib['class'].split(' '):
  22.                             logging.info(t)
  23.                             logging.info('Lauching the matching process')
  24.                             srch_res = re.match(rgx, t)
  25.                             logging.debug('srch_res : '+str(srch_res))
  26.                             if srch_res is not None:
  27.                                
  28.                                 logging.info('Match !')
  29.                                
  30.                                 # Source : http://stackoverflow.com/a/12906014/5527968
  31.                                 tag_class = (tag.attrib['class'].split(' ')[0])
  32.                                 tag_dict[srch_res.group(1)][tag.tag] = [tag_class]
  33.                                 logging.info('tag_dict : ')
  34.                                 logging.info(pprint.pprint(tag_dict))
  35.  
  36.                                 #self._assossiated_list.setdefault(srch_res.group(1), {})[tag.tag] = tag_class
  37.                             else:
  38.                                 continue
  39.                     else:
  40.                         pass
  41.                
  42.                 except Exception as e:
  43.                     logging.info(e)
  44.                     pass
  45.            
  46.                 logging.info(tmp_list)
  47.  
  48.         def find_xml_tag():
  49.             logging.info('Find all xml-tag')
  50.             self._p_list = self._tree.xpath('//*') # For all existing tag
  51.             for p in self._p_list:
  52.                 get_astd(p)
  53.  
  54.         find_xml_tag()
  55.         logging.info(pprint.pprint(self._assossiated_list))
  56.  
  57. # Expected output :
  58. # {'caption': {'p': ['Citation', 'Ct']},
  59. # 'chapter': {'head': 'Chapter'},
  60. # 'niv1': {'h2': 'Stitre1', 'p': 'Stitre1'},
  61. # 'niv2': {'h3': 'Stitre2'}}
  62.  
  63. # Ouput :
  64. # {'caption': {'p': 'Citation'},
  65. # 'chapter': {'head': 'Chapter'},
  66. # 'niv1': {'h2': 'Stitre1', 'p': 'Stitre1'},
  67. # 'niv2': {'h3': 'Stitre2'}}
Advertisement
Add Comment
Please, Sign In to add comment