X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvpp-api%2Fpython%2Fvpp_papi%2Fmacaddress.py;h=66349a3c19a6ebd8aff8df74acc6e90c4837dd11;hb=b10561bbecb2870360a731b4696bff920150720d;hp=a1003812003a2c1172929cbe6ee8ab02afef4387;hpb=8006c6aa425126529b4017768a9201e4f03964ad;p=vpp.git diff --git a/src/vpp-api/python/vpp_papi/macaddress.py b/src/vpp-api/python/vpp_papi/macaddress.py index a1003812003..66349a3c19a 100644 --- a/src/vpp-api/python/vpp_papi/macaddress.py +++ b/src/vpp-api/python/vpp_papi/macaddress.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Copyright (c) 2016 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,20 +18,19 @@ import binascii def mac_pton(s): - '''Convert MAC address as text to binary''' - return binascii.unhexlify(s.replace(':', '')) + """Convert MAC address as text to binary""" + return binascii.unhexlify(s.replace(":", "")) def mac_ntop(binary): - '''Convert MAC address as binary to text''' - x = b':'.join(binascii.hexlify(binary)[i:i + 2] - for i in range(0, 12, 2)) - return str(x.decode('ascii')) + """Convert MAC address as binary to text""" + x = b":".join(binascii.hexlify(binary)[i : i + 2] for i in range(0, 12, 2)) + return str(x.decode("ascii")) -class MACAddress(): +class MACAddress: def __init__(self, mac): - '''MAC Address as a text-string (aa:bb:cc:dd:ee:ff) or 6 bytes''' + """MAC Address as a text-string (aa:bb:cc:dd:ee:ff) or 6 bytes""" # Of course Python 2 doesn't distinguish str from bytes if type(mac) is bytes and len(mac) == 6: self.mac_binary = mac @@ -51,4 +50,20 @@ class MACAddress(): return self.mac_string def __repr__(self): - return '%s(%s)' % (self.__class__.__name__, self.mac_string) + return "%s(%s)" % (self.__class__.__name__, self.mac_string) + + def __eq__(self, other): + if not isinstance(other, MACAddress): + try: + # if it looks like a mac address, we'll take it. + # (allows for equality with scapy hw-addresses) + return self.mac_binary == MACAddress(other).mac_binary + except Exception: + return NotImplemented + return self.mac_binary == other.mac_binary + + def __ne__(self, other): + return not self == other + + def __hash__(self): + return hash(self.mac_binary)