I really enjoy the Django/Jinja style of formatting. I understand wanting to keep logic and presentation separate, but the if statements and for loops are ideal when reporting a changing amount of data.
I am creating a "Report" class that returns some information about the inputs and outputs of a function in a more verbose way, and what I would like is to have a string like:
template="""
INPUTS:
{{% for key,value in inputsdict.items() %}}
{{key}}: {{value}}
{{% endfor #}}
OUTPUTS:
{{% for key,value in outputsdict.items() %}}
{{key}}: {{value}}
{{% endfor #}}
"""
and then I could just call a classic template.format(**kwargs)
I am wondering if Jinja is my best bet to do this, or whether python has a native, lightweight string loop formatting functionality of some kind that I should be looking at.
Edit: Additionally, if Jinja is my best bet, I would really appreciate an example of how to use it outside of a web-oriented environment. The docs are kind of hard to navigate.
Generate your data from your for loops and then put them in the results.
There's no need to over-use the format template system. Just use it for the pieces where it's needed.
inputs = ['%s: %s' % (k, v) for k, v in input_dict.items()]
outputs = ['%s: %s' % (k, v) for k, v in output_dict.items()]
print '\n'.join('INPUTS:', '\n '.join(inputs),
'',
'OUTPUTS:', '\n '.join(outputs))
You could make the inputs
/outputs
creation step a function, or use something from the json library for pretty printing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With