netutils icon indicating copy to clipboard operation
netutils copied to clipboard

Extend ip_network extension to support method calls with kwargs

Open jeffkala opened this issue 1 year ago • 0 comments

ip_network has additional method calls that support additional kwargs to be passed in. This adds that support. For clarity, ip_address and ip_interface don't have these, so the extension is only on ip_network.

Example

>>> list(ipaddress_network('192.168.1.0/28', 'subnets', new_prefix=30))
[IPv4Network('192.168.1.0/30'), IPv4Network('192.168.1.4/30'), IPv4Network('192.168.1.8/30'), IPv4Network('192.168.1.12/30')]

or jinja2

from jinja2 import Environment, select_autoescape
from netutils.utils import jinja2_convenience_function

template_string = """
Example:
{% for sub in "192.168.1.0/28" | ipaddress_network('subnets', new_prefix=30) | list %}
{{ sub.with_netmask }}
{% endfor %}
"""

env = Environment(
    autoescape=select_autoescape()
)
env.filters.update(jinja2_convenience_function())
template = env.from_string(template_string)
result = template.render()
print(result)

>>> print(result)

Example:

192.168.1.0/255.255.255.252

192.168.1.4/255.255.255.252

192.168.1.8/255.255.255.252

192.168.1.12/255.255.255.252

jeffkala avatar Jun 14 '24 21:06 jeffkala