string_interp.py 595 B

1234567891011121314151617181920212223
  1. import re
  2. from macropy.core.macros import *
  3. from macropy.core.hquotes import macros, hq, u, ast_list
  4. macros = Macros()
  5. @macros.expr
  6. def s(tree, **kw):
  7. """Macro to easily interpolate values into string literals."""
  8. captured = []
  9. new_string = ""
  10. chunks = re.split("{(.*?)}", tree.s)
  11. for i in range(0, len(chunks)):
  12. if i % 2 == 0:
  13. new_string += chunks[i]
  14. else:
  15. new_string += "%s"
  16. captured += [chunks[i]]
  17. result = hq[u[new_string] % tuple(ast_list[map(parse_expr, captured)])]
  18. return result