Skip to content

Protocol Mapper

netutils.utils

Utilities for the netutils library.

jinja2_convenience_function()

Convenience function that allows netutils filter to be used easily with jinja2.

Returns:

Type Description
t.Dict[str, t.Callable[..., t.Any]]

Keys are the function names for the Jinja2 filter and values are the function objects.

Examples:

>>> from netutils.utils import jinja2_convenience_function
>>> function_mappings = jinja2_convenience_function()
>>> function_mappings["get_first_usable"]("192.168.0.0/24")
'192.168.0.1'
>>> function_mappings["get_broadcast_address"]("192.168.0.0/24")
'192.168.0.255'
Source code in netutils/utils.py
def jinja2_convenience_function() -> t.Dict[str, t.Callable[..., t.Any]]:
    """Convenience function that allows netutils filter to be used easily with jinja2.

    Returns:
        Keys are the function names for the Jinja2 filter and values are the function objects.

    Examples:
        >>> from netutils.utils import jinja2_convenience_function
        >>> function_mappings = jinja2_convenience_function()
        >>> function_mappings["get_first_usable"]("192.168.0.0/24")
        '192.168.0.1'
        >>> function_mappings["get_broadcast_address"]("192.168.0.0/24")
        '192.168.0.255'
    """
    result = {}

    for jinja2_function_name, function_import_path in _JINJA2_FUNCTION_MAPPINGS.items():
        module, function_name = function_import_path.rsplit(".", 1)
        imported_module = import_module(f"netutils.{module}")
        function_object = getattr(imported_module, function_name)
        result[jinja2_function_name] = function_object
    return result

Review how to use code within the user documentation