Fix pep8 violations in csit/libraries/ipaddr.py
authorThanh Ha <thanh.ha@linuxfoundation.org>
Sun, 15 Mar 2015 19:19:35 +0000 (15:19 -0400)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Mon, 16 Mar 2015 01:03:53 +0000 (21:03 -0400)
Change-Id: Ie59ff4577aa2dac7084ae3d3b1f8a9eda138fbc7
Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
test/csit/libraries/ipaddr.py

index ad27ae9d4f8f5635d69df50ed4256bb50405435d..71a58b832664011ac4bf4ccf8f165b0d85a97b9c 100644 (file)
@@ -167,6 +167,7 @@ def _find_address_range(addresses):
             break
     return (first, last)
 
+
 def _get_prefix_length(number1, number2, bits):
     """Get the number of leading bits that are same for two numbers.
 
@@ -184,6 +185,7 @@ def _get_prefix_length(number1, number2, bits):
             return bits - i
     return 0
 
+
 def _count_righthand_zero_bits(number, bits):
     """Count the number of zero bits on the right hand side.
 
@@ -201,6 +203,7 @@ def _count_righthand_zero_bits(number, bits):
         if (number >> i) % 2:
             return i
 
+
 def summarize_address_range(first, last):
     """Summarize a network range given the first and last IP addresses.
 
@@ -231,7 +234,7 @@ def summarize_address_range(first, last):
         raise TypeError('first and last must be IP addresses, not networks')
     if first.version != last.version:
         raise TypeError("%s and %s are not of the same version" % (
-                str(first), str(last)))
+            str(first), str(last)))
     if first > last:
         raise ValueError('last IP address must be greater than first')
 
@@ -265,6 +268,7 @@ def summarize_address_range(first, last):
         first = IPAddress(first_int, version=first._version)
     return networks
 
+
 def _collapse_address_list_recursive(addresses):
     """Loops through the addresses, collapsing concurrent netblocks.
 
@@ -340,17 +344,17 @@ def collapse_address_list(addresses):
         if isinstance(ip, _BaseIP):
             if ips and ips[-1]._version != ip._version:
                 raise TypeError("%s and %s are not of the same version" % (
-                        str(ip), str(ips[-1])))
+                    str(ip), str(ips[-1])))
             ips.append(ip)
         elif ip._prefixlen == ip._max_prefixlen:
             if ips and ips[-1]._version != ip._version:
                 raise TypeError("%s and %s are not of the same version" % (
-                        str(ip), str(ips[-1])))
+                    str(ip), str(ips[-1])))
             ips.append(ip.ip)
         else:
             if nets and nets[-1]._version != ip._version:
                 raise TypeError("%s and %s are not of the same version" % (
-                        str(ip), str(ips[-1])))
+                    str(ip), str(ips[-1])))
             nets.append(ip)
 
     # sort and dedup
@@ -390,6 +394,7 @@ except (NameError, TypeError):
         def __repr__(self):
             return 'Bytes(%s)' % str.__repr__(self)
 
+
 def get_mixed_type_key(obj):
     """Return a key suitable for sorting between networks and addresses.
 
@@ -414,6 +419,7 @@ def get_mixed_type_key(obj):
         return obj._get_address_key()
     return NotImplemented
 
+
 class _IPAddrBase(object):
 
     """The mother class."""
@@ -475,10 +481,10 @@ class _BaseIP(_IPAddrBase):
     def __lt__(self, other):
         if self._version != other._version:
             raise TypeError('%s and %s are not of the same version' % (
-                    str(self), str(other)))
+                str(self), str(other)))
         if not isinstance(other, _BaseIP):
             raise TypeError('%s and %s are not of the same type' % (
-                    str(self), str(other)))
+                str(self), str(other)))
         if self._ip != other._ip:
             return self._ip < other._ip
         return False
@@ -486,10 +492,10 @@ class _BaseIP(_IPAddrBase):
     def __gt__(self, other):
         if self._version != other._version:
             raise TypeError('%s and %s are not of the same version' % (
-                    str(self), str(other)))
+                str(self), str(other)))
         if not isinstance(other, _BaseIP):
             raise TypeError('%s and %s are not of the same type' % (
-                    str(self), str(other)))
+                str(self), str(other)))
         if self._ip != other._ip:
             return self._ip > other._ip
         return False
@@ -510,7 +516,7 @@ class _BaseIP(_IPAddrBase):
         return '%s(%r)' % (self.__class__.__name__, str(self))
 
     def __str__(self):
-        return  '%s' % self._string_from_ip_int(self._ip)
+        return '%s' % self._string_from_ip_int(self._ip)
 
     def __hash__(self):
         return hash(hex(long(self._ip)))
@@ -574,10 +580,10 @@ class _BaseNet(_IPAddrBase):
     def __lt__(self, other):
         if self._version != other._version:
             raise TypeError('%s and %s are not of the same version' % (
-                    str(self), str(other)))
+                str(self), str(other)))
         if not isinstance(other, _BaseNet):
             raise TypeError('%s and %s are not of the same type' % (
-                    str(self), str(other)))
+                str(self), str(other)))
         if self.network != other.network:
             return self.network < other.network
         if self.netmask != other.netmask:
@@ -587,10 +593,10 @@ class _BaseNet(_IPAddrBase):
     def __gt__(self, other):
         if self._version != other._version:
             raise TypeError('%s and %s are not of the same version' % (
-                    str(self), str(other)))
+                str(self), str(other)))
         if not isinstance(other, _BaseNet):
             raise TypeError('%s and %s are not of the same type' % (
-                    str(self), str(other)))
+                str(self), str(other)))
         if self.network != other.network:
             return self.network > other.network
         if self.netmask != other.netmask:
@@ -626,8 +632,8 @@ class _BaseNet(_IPAddrBase):
         return not eq
 
     def __str__(self):
-        return  '%s/%s' % (str(self.ip),
-                           str(self._prefixlen))
+        return '%s/%s' % (str(self.ip),
+                          str(self._prefixlen))
 
     def __hash__(self):
         return hash(int(self.network) ^ int(self.netmask))
@@ -635,7 +641,7 @@ class _BaseNet(_IPAddrBase):
     def __contains__(self, other):
         # always false if one is v4 and the other is v6.
         if self._version != other._version:
-          return False
+            return False
         # dealing with another network.
         if isinstance(other, _BaseNet):
             return (self.network <= other.network and
@@ -751,7 +757,7 @@ class _BaseNet(_IPAddrBase):
 
         # Make sure we're comparing the network of other.
         other = IPNetwork('%s/%s' % (str(other.network), str(other.prefixlen)),
-                   version=other._version)
+                          version=other._version)
 
         s1, s2 = self.subnet()
         while s1 != other and s2 != other:
@@ -763,7 +769,7 @@ class _BaseNet(_IPAddrBase):
                 s1, s2 = s2.subnet()
             else:
                 # If we got here, there's a bug somewhere.
-                assert True == False, ('Error performing exclusion: '
+                assert True is False, ('Error performing exclusion: '
                                        's1: %s s2: %s other: %s' %
                                        (str(s1), str(s2), str(other)))
         if s1 == other:
@@ -772,7 +778,7 @@ class _BaseNet(_IPAddrBase):
             ret_addrs.append(s1)
         else:
             # If we got here, there's a bug somewhere.
-            assert True == False, ('Error performing exclusion: '
+            assert True is False, ('Error performing exclusion: '
                                    's1: %s s2: %s other: %s' %
                                    (str(s1), str(s2), str(other)))
 
@@ -934,7 +940,7 @@ class _BaseNet(_IPAddrBase):
 
         first = IPNetwork('%s/%s' % (str(self.network),
                                      str(self._prefixlen + prefixlen_diff)),
-                         version=self._version)
+                          version=self._version)
 
         yield first
         current = first
@@ -988,7 +994,6 @@ class _BaseNet(_IPAddrBase):
                 raise ValueError('cannot set prefixlen_diff and new_prefix')
             prefixlen_diff = self._prefixlen - new_prefix
 
-
         if self.prefixlen - prefixlen_diff < 0:
             raise ValueError(
                 'current prefixlen is %d, cannot have a prefixlen_diff of %d' %
@@ -1104,14 +1109,14 @@ class _BaseV4(object):
 
     @property
     def is_reserved(self):
-       """Test if the address is otherwise IETF reserved.
+        """Test if the address is otherwise IETF reserved.
 
         Returns:
             A boolean, True if the address is within the
             reserved IPv4 Network range.
 
-       """
-       return self in IPv4Network('240.0.0.0/4')
+        """
+        return self in IPv4Network('240.0.0.0/4')
 
     @property
     def is_private(self):
@@ -1297,13 +1302,12 @@ class IPv4Network(_BaseV4, _BaseNet):
                 # We have dotted decimal netmask.
                 if self._is_valid_netmask(addr[1]):
                     self.netmask = IPv4Address(self._ip_int_from_string(
-                            addr[1]))
+                        addr[1]))
                 elif self._is_hostmask(addr[1]):
                     self.netmask = IPv4Address(
                         self._ip_int_from_string(addr[1]) ^ self._ALL_ONES)
                 else:
-                    raise NetmaskValueError('%s is not a valid netmask'
-                                                     % addr[1])
+                    raise NetmaskValueError('%s is not a valid netmask' % addr[1])
 
                 self._prefixlen = self._prefix_from_ip_int(int(self.netmask))
             else:
@@ -1361,8 +1365,7 @@ class IPv4Network(_BaseV4, _BaseNet):
         if len(mask) == 4:
             if [x for x in mask if int(x) not in self._valid_mask_octets]:
                 return False
-            if [y for idx, y in enumerate(mask) if idx > 0 and
-                y > mask[idx - 1]]:
+            if [y for idx, y in enumerate(mask) if idx > 0 and y > mask[idx - 1]]:
                 return False
             return True
         try:
@@ -1791,7 +1794,6 @@ class IPv6Address(_BaseV6, _BaseIP):
 
 
 class IPv6Network(_BaseV6, _BaseNet):
-
     """This class represents and manipulates 128-bit IPv6 networks.
 
     Attributes: [examples for IPv6('2001:658:22A:CAFE:200::1/64')]
@@ -1804,7 +1806,6 @@ class IPv6Network(_BaseV6, _BaseNet):
 
     """
 
-
     def __init__(self, address, strict=False):
         """Instantiate a new IPv6 Network object.