proposal to allow to set the delimiter in str.format to something other than curly bracket

Alia Khouri alia_khouri at yahoo.com
Mon Apr 4 16:45:17 EDT 2011


Peter Otten wrote:
> You could automatically convert from a custom format like that in your
> original post:
>
> import re
>
> _lookup = {
>     "[[": "{",
>     "]]": "}",
>     "{": "{{",
>     "}": "}}",
>
> }
>
> def _substitute(m):
>     return _lookup[m.group()]
>
> def custom_format(template, *args, **kw):
>     return (re.compile(r"\[\[|]]|\{|\}")
>             .sub(_substitute, template)
>             .format(*args, **kw))
>
> code = "class [[0]]Model { public bool IsModel(){ return a[42] || true; } }"
> print custom_format(code, "My")

Nice! I didn't think of that. I guess I could get some additional
performance by taking the re.compile step out of the function. Thanks
for the tip!

AK

AK





More information about the Python-list mailing list