def safe_ip_format(ip):
 try:
  if (netaddr.IPAddress(ip).version == 6):
   return ('[%s]' % ip)
 except (TypeError, netaddr.AddrFormatError):
  pass
 return ip

def safe_ip_format ( ip ) : try : if ( netaddr . IPAddress ( ip ) . version == 6 ) : return ( '[%s]' % ip ) except ( TypeError , netaddr . AddrFormatError ) : pass return ip
transform ip string to "safe" format .