Guest User

Untitled

a guest
Jan 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. import string
  2.  
  3. class MyForm(string.Formatter):
  4. def parse(self, format_string):
  5. for tpl in string.Formatter.parse(self, format_string):
  6. print(tpl)
  7. literal_text, field_name, format_spec, conversion = tpl
  8. if field_name:
  9. literal_text += f'"{field_name}"'
  10. yield literal_text, None, None, None
  11. else:
  12. yield tpl
  13.  
  14. fmt = '{abc} {def} {3} {4} foo bar not in brackets {hello} {5,7} end'
  15. print(fmt)
  16. x = MyForm()
  17. print(x.format(fmt))
  18.  
  19. """
  20.  
  21. {abc} {def} {3} {4} foo bar not in brackets {hello} {5,7} end
  22. ('', 'abc', '', None)
  23. (' ', 'def', '', None)
  24. (' ', '3', '', None)
  25. (' ', '4', '', None)
  26. (' foo bar not in brackets ', 'hello', '', None)
  27. (' ', '5,7', '', None)
  28. (' end', None, None, None)
  29. "abc" "def" "3" "4" foo bar not in brackets "hello" "5,7" end
  30.  
  31. """
Add Comment
Please, Sign In to add comment