def netmask_to_cidr(dq):
 if isinstance(dq, basestring):
  dq = IPAddr(dq)
 v = dq.toUnsigned(networkOrder=False)
 c = 0
 while (v & 2147483648):
  c += 1
  v <<= 1
 v = (v & 4294967295)
 if (v != 0):
  raise RuntimeError(('Netmask %s is not CIDR-compatible' % (dq,)))
 return c

def netmask_to_cidr ( dq ) : if isinstance ( dq , basestring ) : dq = IPAddr ( dq ) v = dq . toUnsigned ( networkOrder = False ) c = 0 while ( v & 2147483648 ) : c += 1 v <<= 1 v = ( v & 4294967295 ) if ( v != 0 ) : raise RuntimeError ( ( 'Netmask %s is not CIDR-compatible' % ( dq , ) ) ) return c
takes a netmask as either an ipaddr or a string .