xoutil.formatter
- Formatting¶
Smart formatting.
-
class
xoutil.formatter.
Template
(template)[source]¶ A string class for supporting $-substitutions.
It has similar interface that string.Template but using “eval” instead simple dictionary looking.
This means that you get all the functionality provided by string.Template (although, perhaps modified) and you get also the ability to write more complex expressions.
If you need repetition or other flow-control sentences you should use other templating system.
If you enclose and expression within
${?...}
it will be evaluated as a python expression. Simple variables are allowed just with$var
or${var}
:>>> tpl = Template(str('${?1 + 1} is 2, and ${?x + x} is $x + ${x}')) >>> (tpl % dict(x=4)) == '2 is 2, and 8 is 4 + 4' True
The mapping may be given by calling the template:
>>> tpl(x=5) == '2 is 2, and 10 is 5 + 5' True