fork(4) download
  1. import string
  2.  
  3. class FormatPlaceholder:
  4. def __init__(self, key):
  5. self.key = key
  6. def __format__(self, spec):
  7. result = self.key
  8. if spec:
  9. result += ":" + spec
  10. return "{" + result + "}"
  11.  
  12. class FormatDict(dict):
  13. def __missing__(self, key):
  14. return FormatPlaceholder(key)
  15.  
  16. s = '{foo} {bar} {baz:123xyz}'
  17. formatter = string.Formatter()
  18. mapping = FormatDict(foo='FOO')
  19. print(formatter.vformat(s, (), mapping))
Success #stdin #stdout 0.05s 10376KB
stdin
Standard input is empty
stdout
FOO {bar} {baz:123xyz}