PythonExtension

Description

PythonExtension provides the capability to do actions on the python version string, but also on Python dictionaries.

class jinjazzle.ext.python.PythonExtension(environment)

Jinja2 extension for python manipulation.

jinjazzle.ext.python.pyversion(lhs)

Return a Python Major.Minor version string and return it as a tuple (Major, Minor).

Available as a function and a filter. For the latter case, lhs is automatically specified as the left hand side of the filter.

Parameters:

lhs – Version string. eg: “3.9”

# scaffold.python_min_version is "3.9" for example
{% if (scaffold.python_min_version | pyversion)[1] <= 9 %}

{% if (pyversion("3.9")[1] == 9 %}
jinjazzle.ext.python.pyversion_format(lhs, fmt)

Return a Python Major.Minor version string formatted according to format passed as parameter.

Internally uses str.format().

Available as a function and a filter. For the latter case, lhs is automatically specified as the left hand side of the filter.

Parameters:
  • lhs – Version string. eg: “3.9”

  • fmt – Format string. Available replacements: major, minor

# Returns "3.9-dev"
{{ "3.9" | pyversion_format("Python {major}.{minor}-dev") }}
jinjazzle.ext.python.pyversion_sequence(lhs, stop, sep, fmt)

Return a sequence of Python Major.Minor version strings formatted according to format passed as parameter.

Internally uses str.format().

Available as a function and a filter. For the latter case, lhs is automatically specified as the left hand side of the filter.

Parameters:
  • lhs – Version string. eg: “3.9”

  • stop – Generate sequence from minor version, to this value (included)

  • fmt – Format string. Available replacements: major, minor

  • sep – Separator to use for joining the formatted strings.

# Returns "3.9 3.10 3.11 3.12"
{{ "3.9" | pyversion_sequence(12) }}
jinjazzle.ext.python.to_json(lhs, sort_keys=True, **kwargs)

Converts a Python dictionary to a JSON string.

Internally uses json.dumps().

Available as a function and a filter. For the latter case, lhs is automatically specified as the left hand side of the filter.

Parameters:
  • lhs – Python dictionary

  • sort_keys – Output of dictionaries will be sorted by key

  • kwargs – Any of the values allowed for json.dumps()

# Returns '{"foobar": 3}'
{{ {'foobar':3} | to_json }}