Functional Weave
Code in Python

net.cidr@1.0.0

impl/python/prefix_to_netmask.py

405 bytes · the Python implementation · view raw

from .net_ipv4_format_ipv4 import format_ipv4


def prefix_to_netmask(prefix: int) -> str:
    """The dotted-quad mask for a prefix length: 24 is 255.255.255.0."""
    if isinstance(prefix, bool) or not isinstance(prefix, int) or prefix < 0 or prefix > 32:
        raise ValueError("prefix must be an integer from 0 to 32, received %r" % (prefix,))
    return format_ipv4(4294967296 - 2 ** (32 - prefix))