Skip to content

Instantly share code, notes, and snippets.

@gmacon
Created May 12, 2018 15:43
Show Gist options
  • Save gmacon/5145678ebfa808b4905215f58f1769ac to your computer and use it in GitHub Desktop.
Save gmacon/5145678ebfa808b4905215f58f1769ac to your computer and use it in GitHub Desktop.
import ipaddress
import hypothesis.strategies as S
@S.compound
def ip4_networks(draw):
prefixlen = draw(S.integers(min_value=0, max_value=32))
if prefixlen:
network_bits = draw(S.integers(min_value=0, max_value=(1 << prefixlen) - 1))
else:
network_bits = 0
network = network_bits << (32 - prefixlen)
return ipaddress.IPv4Network((network, prefixlen))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment