Closed t92549 closed 1 year ago
Example usage of jinja:
from jinja2.nativetypes import NativeEnvironment
env = NativeEnvironment()
def capitalize(input):
return input.capitalize()
function_dict = {
"capitalize": capitalize
}
t = env.from_string("def __init__(self, {% for input in inputs %}{{ capitalize(input) }}=None, {% endfor %}options=None):")
t.globals.update(function_dict)
print(t.render(inputs=["input", "skip", "validate"]))
'def __init__(self, Input=None, Skip=None, Validate=None, options=None):'
The capitalize function is just there to show how custom functions work. In real usage, I think a different Environment would be used, and you would use a template file rather than a string.
jinja is a template engine for Python. It could be used to make the fishbowl code clearer, more extensible and maintainable.