You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

macaddress

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

macaddress - pypi Package Compare versions

Comparing version
1.0.1
to
1.1.0
+12
LICENSE
Copyright 2021 Alexander Kozhevnikov <mentalisttraceur@gmail.com>
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+45
-36
Metadata-Version: 1.1
Name: macaddress
Version: 1.0.1
Version: 1.1.0
Summary: Like ``ipaddress``, but for hardware identifiers such as MAC addresses.

@@ -96,10 +96,8 @@ Home-page: https://github.com/mentalisttraceur/python-macaddress

>>> macaddress.MAC('foo bar')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/code/m/macaddress.py", line 84, in __init__
self._address, _ = _parse(address, type(self))
File "/home/user/code/m/macaddress.py", line 357, in _parse
raise _value_error(input, 'cannot be parsed as', *classes)
ValueError: 'foo bar' cannot be parsed as MAC
>>> try:
... macaddress.MAC('foo bar')
... except ValueError as error:
... print(error)
...
'foo bar' cannot be parsed as MAC

@@ -116,3 +114,2 @@ If you need to parse in a format that isn't supported,

... 'xxxx.xxxx.xxxx.',
... 'xxxxxxxxxxxx',
... )

@@ -131,26 +128,36 @@ ...

>>> macaddress.parse('01:02:03', macaddress.OUI, macaddress.MAC)
>>> from macaddress import EUI48, EUI64, MAC, OUI
>>> macaddress.parse('01:02:03', OUI, MAC)
OUI('01-02-03')
>>> macaddress.parse('01:02:03:04:05:06', macaddress.OUI, macaddress.MAC)
>>> macaddress.parse('01:02:03:04:05:06', OUI, MAC, EUI64)
MAC('01-02-03-04-05-06')
>>> macaddress.parse('010203040506', macaddress.EUI64, macaddress.EUI48)
>>> macaddress.parse('010203040506', EUI64, EUI48)
EUI48('01-02-03-04-05-06')
>>> macaddress.parse('0102030405060708', macaddress.EUI64, macaddress.EUI48)
>>> macaddress.parse('0102030405060708', EUI64, EUI48, OUI, MAC)
EUI64('01-02-03-04-05-06-07-08')
Note that the message of the ``ValueError`` tries to be helpful
to humans by mentioning what classes you tried to parse it as:
If the input string cannot be parsed as any of
the given classes, a ``ValueError`` is raised:
.. code:: python
>>> macaddress.parse('x', macaddress.MAC, macaddress.OUI, macaddress.EUI64)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/code/m/macaddress.py", line 335, in parse
address, cls = _parse(string, *classes)
File "/home/user/code/m/macaddress.py", line 363, in _parse
raise _value_error(input, 'cannot be parsed as', *classes)
ValueError: 'x' cannot be parsed as MAC, OUI, or EUI64
>>> try:
... macaddress.parse('01:23', MAC, OUI)
... except ValueError as error:
... print(error)
...
'01:23' cannot be parsed as MAC or OUI
>>> try:
... macaddress.parse('01:23', MAC, OUI, EUI64)
... except ValueError as error:
... print(error)
...
'01:23' cannot be parsed as MAC, OUI, or EUI64
Note that the message of the ``ValueError`` tries to be helpful
for developers, but it is not localized, nor is its exact text
part of the official public interface covered by SemVer.
Parse from Bytes

@@ -172,8 +179,8 @@ ~~~~~~~~~~~~~~~~

>>> macaddress.MAC(b'\x01\x02\x03')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/code/m/macaddress.py", line 86, in __init__
raise _value_error(address, 'has wrong length for', type(self))
ValueError: b'\x01\x02\x03' has wrong length for MAC
>>> try:
... macaddress.MAC(b'\x01\x02\x03')
... except ValueError as error:
... print(error)
...
b'\x01\x02\x03' has wrong length for MAC

@@ -209,8 +216,8 @@

>>> macaddress.OUI(1_000_000_000)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/code/m/macaddress.py", line 78, in __init__
raise _value_error(address, 'is too big for', type(self))
ValueError: 1000000000 is too big for OUI
>>> try:
... macaddress.OUI(1_000_000_000)
... except ValueError as error:
... print(error)
...
1000000000 is too big for OUI

@@ -245,2 +252,4 @@

AB:CD:EF:01:02:03
>>> str(MACWithColonsByDefault(int(mac)))
01:02:03:04:05:06

@@ -247,0 +256,0 @@ Note that appending the original ``formats``

@@ -0,1 +1,2 @@

LICENSE
README.rst

@@ -2,0 +3,0 @@ macaddress.py

@@ -15,3 +15,3 @@ # SPDX-License-Identifier: 0BSD

)
__version__ = '1.0.1'
__version__ = '1.1.0'

@@ -26,15 +26,24 @@

def _value_error(value, error, *classes):
def _class_names_in_proper_english(classes):
class_names = [cls.__name__ for cls in classes]
number_of_classes = len(classes)
if number_of_classes < 2:
class_description = class_names[0]
return class_names[0]
elif number_of_classes == 2:
class_description = ' or '.join(class_names)
return ' or '.join(class_names)
else:
class_names[-1] = 'or ' + class_names[-1]
class_description = ', '.join(class_names)
return ValueError(repr(value) + ' ' + error + ' ' + class_description)
return ', '.join(class_names)
def _type_error(value, *classes):
class_names = _class_names_in_proper_english(classes)
return TypeError(repr(value) + ' has wrong type for ' + class_names)
def _value_error(value, error, *classes):
class_names = _class_names_in_proper_english(classes)
return ValueError(repr(value) + ' ' + error + ' ' + class_names)
class HWAddress:

@@ -65,6 +74,8 @@ """Base class for hardware addresses.

address: An ``int``, ``bytes``, or ``str`` representation
of the address. If a string, it is parsed using the
``formats`` attribute of the class. If a byte string,
it is read in big-endian. If it is an integer, bytes
in the integer value are used as the address bytes.
of the address, or another instance of the same class
of address. If a string, the ``formats`` attribute of
the class is used to parse it. If a byte string, it
is read in big-endian. If an integer, its value bytes
in big-endian are used as the address bytes. If an
instance of the same address class, its value is used.

@@ -94,4 +105,6 @@ Raises:

self._address, _ = _parse(address, type(self))
elif isinstance(address, type(self)):
self._address = address._address
else:
raise TypeError(_name(self) + ' cannot parse ' + _name(address))
raise _type_error(address, type(self))

@@ -306,7 +319,5 @@ def __repr__(self):

def parse(string, *classes):
"""Try parsing a string as several hardware address classes at once.
def parse(value, *classes):
"""Try parsing a value as several hardware address classes at once.
This is useful when you have one piece of code that can accept user
input of two or more different hardware addresses or identifiers.
This lets you can just write

@@ -319,6 +330,6 @@

try:
hw_address = hwaddress.EUI64(user_input)
address = hwaddress.EUI64(user_input)
except ValueError:
try:
hw_address = hwaddress.EUI48(user_input)
address = hwaddress.EUI48(user_input)
except ValueError:

@@ -328,20 +339,34 @@ ...

Arguments:
string: The string to parse as a hardware address.
value: The value to parse as a hardware address. Either a
string, byte string, or an instance of one of the classes.
*classes: HWAddress subclasses to try to parse the string as.
If the input address could parse as more than one of the
classes, it is parsed as the first one.
Returns:
HWAddress: The parsed hardware address.
None: If no classes were passed in.
HWAddress: The parsed hardware address if the value argument
was a string or byte string, or the value argument itself
if it was already an instance of one of the classes.
Raises:
ValueError: If the string could not be parsed as any of the
given classes.
TypeError: If the string is not actually a string.
TypeError: If the value is not one of the valid types,
or if no classes were passed in.
ValueError: If the value could not be parsed as any
of the given classes.
"""
if not isinstance(string, str):
return TypeError
if not classes:
return None
address, cls = _parse(string, *classes)
return cls(address)
raise TypeError('parse() requires at least one class argument')
if isinstance(value, str):
address, cls = _parse(value, *classes)
return cls(address)
elif isinstance(value, bytes):
max_size = len(value) * 8
min_size = max_size - 7
for cls in classes:
if min_size <= cls.size <= max_size:
return cls(value)
raise _value_error(value, 'has wrong length for', *classes)
elif isinstance(value, classes):
return value
raise _type_error(value, *classes)

@@ -348,0 +373,0 @@

+45
-36
Metadata-Version: 1.1
Name: macaddress
Version: 1.0.1
Version: 1.1.0
Summary: Like ``ipaddress``, but for hardware identifiers such as MAC addresses.

@@ -96,10 +96,8 @@ Home-page: https://github.com/mentalisttraceur/python-macaddress

>>> macaddress.MAC('foo bar')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/code/m/macaddress.py", line 84, in __init__
self._address, _ = _parse(address, type(self))
File "/home/user/code/m/macaddress.py", line 357, in _parse
raise _value_error(input, 'cannot be parsed as', *classes)
ValueError: 'foo bar' cannot be parsed as MAC
>>> try:
... macaddress.MAC('foo bar')
... except ValueError as error:
... print(error)
...
'foo bar' cannot be parsed as MAC

@@ -116,3 +114,2 @@ If you need to parse in a format that isn't supported,

... 'xxxx.xxxx.xxxx.',
... 'xxxxxxxxxxxx',
... )

@@ -131,26 +128,36 @@ ...

>>> macaddress.parse('01:02:03', macaddress.OUI, macaddress.MAC)
>>> from macaddress import EUI48, EUI64, MAC, OUI
>>> macaddress.parse('01:02:03', OUI, MAC)
OUI('01-02-03')
>>> macaddress.parse('01:02:03:04:05:06', macaddress.OUI, macaddress.MAC)
>>> macaddress.parse('01:02:03:04:05:06', OUI, MAC, EUI64)
MAC('01-02-03-04-05-06')
>>> macaddress.parse('010203040506', macaddress.EUI64, macaddress.EUI48)
>>> macaddress.parse('010203040506', EUI64, EUI48)
EUI48('01-02-03-04-05-06')
>>> macaddress.parse('0102030405060708', macaddress.EUI64, macaddress.EUI48)
>>> macaddress.parse('0102030405060708', EUI64, EUI48, OUI, MAC)
EUI64('01-02-03-04-05-06-07-08')
Note that the message of the ``ValueError`` tries to be helpful
to humans by mentioning what classes you tried to parse it as:
If the input string cannot be parsed as any of
the given classes, a ``ValueError`` is raised:
.. code:: python
>>> macaddress.parse('x', macaddress.MAC, macaddress.OUI, macaddress.EUI64)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/code/m/macaddress.py", line 335, in parse
address, cls = _parse(string, *classes)
File "/home/user/code/m/macaddress.py", line 363, in _parse
raise _value_error(input, 'cannot be parsed as', *classes)
ValueError: 'x' cannot be parsed as MAC, OUI, or EUI64
>>> try:
... macaddress.parse('01:23', MAC, OUI)
... except ValueError as error:
... print(error)
...
'01:23' cannot be parsed as MAC or OUI
>>> try:
... macaddress.parse('01:23', MAC, OUI, EUI64)
... except ValueError as error:
... print(error)
...
'01:23' cannot be parsed as MAC, OUI, or EUI64
Note that the message of the ``ValueError`` tries to be helpful
for developers, but it is not localized, nor is its exact text
part of the official public interface covered by SemVer.
Parse from Bytes

@@ -172,8 +179,8 @@ ~~~~~~~~~~~~~~~~

>>> macaddress.MAC(b'\x01\x02\x03')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/code/m/macaddress.py", line 86, in __init__
raise _value_error(address, 'has wrong length for', type(self))
ValueError: b'\x01\x02\x03' has wrong length for MAC
>>> try:
... macaddress.MAC(b'\x01\x02\x03')
... except ValueError as error:
... print(error)
...
b'\x01\x02\x03' has wrong length for MAC

@@ -209,8 +216,8 @@

>>> macaddress.OUI(1_000_000_000)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/code/m/macaddress.py", line 78, in __init__
raise _value_error(address, 'is too big for', type(self))
ValueError: 1000000000 is too big for OUI
>>> try:
... macaddress.OUI(1_000_000_000)
... except ValueError as error:
... print(error)
...
1000000000 is too big for OUI

@@ -245,2 +252,4 @@

AB:CD:EF:01:02:03
>>> str(MACWithColonsByDefault(int(mac)))
01:02:03:04:05:06

@@ -247,0 +256,0 @@ Note that appending the original ``formats``

@@ -88,10 +88,8 @@ macaddress

>>> macaddress.MAC('foo bar')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/code/m/macaddress.py", line 84, in __init__
self._address, _ = _parse(address, type(self))
File "/home/user/code/m/macaddress.py", line 357, in _parse
raise _value_error(input, 'cannot be parsed as', *classes)
ValueError: 'foo bar' cannot be parsed as MAC
>>> try:
... macaddress.MAC('foo bar')
... except ValueError as error:
... print(error)
...
'foo bar' cannot be parsed as MAC

@@ -108,3 +106,2 @@ If you need to parse in a format that isn't supported,

... 'xxxx.xxxx.xxxx.',
... 'xxxxxxxxxxxx',
... )

@@ -123,26 +120,36 @@ ...

>>> macaddress.parse('01:02:03', macaddress.OUI, macaddress.MAC)
>>> from macaddress import EUI48, EUI64, MAC, OUI
>>> macaddress.parse('01:02:03', OUI, MAC)
OUI('01-02-03')
>>> macaddress.parse('01:02:03:04:05:06', macaddress.OUI, macaddress.MAC)
>>> macaddress.parse('01:02:03:04:05:06', OUI, MAC, EUI64)
MAC('01-02-03-04-05-06')
>>> macaddress.parse('010203040506', macaddress.EUI64, macaddress.EUI48)
>>> macaddress.parse('010203040506', EUI64, EUI48)
EUI48('01-02-03-04-05-06')
>>> macaddress.parse('0102030405060708', macaddress.EUI64, macaddress.EUI48)
>>> macaddress.parse('0102030405060708', EUI64, EUI48, OUI, MAC)
EUI64('01-02-03-04-05-06-07-08')
Note that the message of the ``ValueError`` tries to be helpful
to humans by mentioning what classes you tried to parse it as:
If the input string cannot be parsed as any of
the given classes, a ``ValueError`` is raised:
.. code:: python
>>> macaddress.parse('x', macaddress.MAC, macaddress.OUI, macaddress.EUI64)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/code/m/macaddress.py", line 335, in parse
address, cls = _parse(string, *classes)
File "/home/user/code/m/macaddress.py", line 363, in _parse
raise _value_error(input, 'cannot be parsed as', *classes)
ValueError: 'x' cannot be parsed as MAC, OUI, or EUI64
>>> try:
... macaddress.parse('01:23', MAC, OUI)
... except ValueError as error:
... print(error)
...
'01:23' cannot be parsed as MAC or OUI
>>> try:
... macaddress.parse('01:23', MAC, OUI, EUI64)
... except ValueError as error:
... print(error)
...
'01:23' cannot be parsed as MAC, OUI, or EUI64
Note that the message of the ``ValueError`` tries to be helpful
for developers, but it is not localized, nor is its exact text
part of the official public interface covered by SemVer.
Parse from Bytes

@@ -164,8 +171,8 @@ ~~~~~~~~~~~~~~~~

>>> macaddress.MAC(b'\x01\x02\x03')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/code/m/macaddress.py", line 86, in __init__
raise _value_error(address, 'has wrong length for', type(self))
ValueError: b'\x01\x02\x03' has wrong length for MAC
>>> try:
... macaddress.MAC(b'\x01\x02\x03')
... except ValueError as error:
... print(error)
...
b'\x01\x02\x03' has wrong length for MAC

@@ -201,8 +208,8 @@

>>> macaddress.OUI(1_000_000_000)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/code/m/macaddress.py", line 78, in __init__
raise _value_error(address, 'is too big for', type(self))
ValueError: 1000000000 is too big for OUI
>>> try:
... macaddress.OUI(1_000_000_000)
... except ValueError as error:
... print(error)
...
1000000000 is too big for OUI

@@ -237,2 +244,4 @@

AB:CD:EF:01:02:03
>>> str(MACWithColonsByDefault(int(mac)))
01:02:03:04:05:06

@@ -239,0 +248,0 @@ Note that appending the original ``formats``