def sanitize_network(network, ip_version):
import ipaddress<EOL>if typepy.is_null_string(network) or network.lower() == "<STR_LIT>":<EOL><INDENT>return get_anywhere_network(ip_version)<EOL><DEDENT>try:<EOL><INDENT>if ip_version == <NUM_LIT:4>:<EOL><INDENT>ipaddress.IPv4Address(network)<EOL>return network + "<STR_LIT>"<EOL><DEDENT>if ip_version == <NUM_LIT:6>:<EOL><INDENT>return ipaddress.IPv6Address(network).compressed<EOL><DEDENT><DEDENT>except ipaddress.AddressValueError:<EOL><INDENT>pass<EOL><DEDENT>if ip_version == <NUM_LIT:4>:<EOL><INDENT>return ipaddress.IPv4Network(six.text_type(network)).compressed<EOL><DEDENT>if ip_version == <NUM_LIT:6>:<EOL><INDENT>return ipaddress.IPv6Network(six.text_type(network)).compressed<EOL><DEDENT>raise ValueError("<STR_LIT>".format(ip_version))<EOL>
:return: Network string
:rtype: str
:raises ValueError: if the network string is invalid.