The little things give you away... A collection of various small helper stuff
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

22 lignes
779 B

  1. #!/usr/bin/env python3
  2. import json
  3. import sys
  4. import xml.etree.ElementTree
  5. root = xml.etree.ElementTree.fromstring(sys.stdin.read())
  6. assert root.tag == 'files'
  7. for file in root:
  8. assert file.tag == 'file'
  9. attributes = file.attrib
  10. childrenTags = [child.tag for child in file]
  11. assert sorted(childrenTags) == sorted(set(childrenTags)), 'duplicate children'
  12. children = {child.tag: child.text for child in file}
  13. assert not any(k in children for k in attributes), 'attribute found in children'
  14. assert not any(k in attributes for k in children), 'child found in attributes'
  15. assert 'name' in attributes, 'malformed file without name attribute'
  16. o = {'name': attributes['name']}
  17. del attributes['name']
  18. o.update(sorted({**attributes, **children}.items()))
  19. print(json.dumps(o))