Advertisement
Guest User

Untitled

a guest
Jun 28th, 2022
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. import string
  2.  
  3. def iter_field_boundaries(s):
  4. idx = 0
  5. for literal, field, spec, conversion in string.Formatter().parse(s):
  6. if literal.startswith("}"):
  7. idx += 1
  8. if literal.endswith("{"):
  9. idx += 1
  10. idx += len(literal)
  11. if field is not None:
  12. right = idx + len(field) + 1
  13. yield (idx, right)
  14. idx = right + 1
  15.  
  16. def point_at_field_boundaries_with_carets(s):
  17. arrows = [" "]*len(s)
  18. for t in iter_field_boundaries(s):
  19. for idx in t:
  20. arrows[idx] = "^"
  21. print(s)
  22. print("".join(arrows))
  23.  
  24. s = "A: {} B: {x} C: {y[0]} D: {z[q]} E: {z[}]} F: {{not a field}}"
  25. point_at_field_boundaries_with_carets(s)
  26.  
  27. #output:
  28. #A: {} B: {x} C: {y[0]} D: {z[q]} E: {z[}]} F: {{not a field}}
  29. # ^^ ^ ^ ^ ^ ^ ^ ^ ^
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement