This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

41 строка
1.2 KiB

  1. #
  2. # This example demonstrates an experimental feature: Text reconstruction
  3. # The Reconstructor takes a parse tree (already filtered from punctuation, of course),
  4. # and reconstructs it into correct text, that can be parsed correctly.
  5. # It can be useful for creating "hooks" to alter data before handing it to other parsers. You can also use it to generate samples from scratch.
  6. #
  7. import json
  8. from .json_parser import json_grammar
  9. from lark import Lark
  10. from lark.reconstruct import Reconstructor
  11. def test():
  12. test_json = '''
  13. {
  14. "empty_object" : {},
  15. "empty_array" : [],
  16. "booleans" : { "YES" : true, "NO" : false },
  17. "numbers" : [ 0, 1, -2, 3.3, 4.4e5, 6.6e-7 ],
  18. "strings" : [ "This", [ "And" , "That" ] ],
  19. "nothing" : null
  20. }
  21. '''
  22. json_parser = Lark(json_grammar)
  23. tree = json_parser.parse(test_json)
  24. # print '@@', tree.pretty()
  25. # for x in tree.find_data('true'):
  26. # x.data = 'false'
  27. # # x.children[0].value = '"HAHA"'
  28. new_json = Reconstructor(json_parser).reconstruct(tree)
  29. print new_json
  30. print json.loads(new_json) == json.loads(test_json)
  31. test()