Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
On branch master
Browse files Browse the repository at this point in the history
 Changes to be committed:

	modified:   macaddress/__init__.py
	modified:   macaddress/ei48.py
	modified:   macaddress/octet.py
	modified:   setup.py
  • Loading branch information
critical-path committed Jun 2, 2019
1 parent cd4d69c commit 73bc213
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion macaddress/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

__author__ = "critical-path"

__version__ = "0.7.0"
__version__ = "0.8.0"

__all__ = [
"ExtendedIdentifier48",
Expand Down
37 changes: 18 additions & 19 deletions macaddress/ei48.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
from macaddress.octet import Octet


PLAIN = re.compile("^[0-9A-Fa-f]{12}$")
HYPHEN = re.compile("^([0-9A-Fa-f]{2}[-]{1}){5}[0-9A-Fa-f]{2}$")
COLON = re.compile("^([0-9A-Fa-f]{2}[:]{1}){5}[0-9A-Fa-f]{2}$")
DOT = re.compile("^([0-9A-Fa-f]{4}[.]{1}){2}[0-9A-Fa-f]{4}$")
NOT_DIGITS = re.compile("[^0-9A-Fa-f]")
TWO_DIGITS = re.compile("[0-9a-f]{2}")
FOUR_DIGITS = re.compile("[0-9a-f]{4}")


class IdentifierError(Exception):
"""
ExtendedIdentifier48 raises IdentifierError if instantiated
Expand Down Expand Up @@ -129,34 +138,27 @@ def is_valid(self):
# It must contain 12 hexadecimal digits, and it may
# be in plain, hyphen, colon, or dot notation.

plain = re.compile("^[0-9A-Fa-f]{12}$")
hyphen = re.compile("^([0-9A-Fa-f]{2}[-]{1}){5}[0-9A-Fa-f]{2}$")
colon = re.compile("^([0-9A-Fa-f]{2}[:]{1}){5}[0-9A-Fa-f]{2}$")
dot = re.compile("^([0-9A-Fa-f]{4}[.]{1}){2}[0-9A-Fa-f]{4}$")

if plain.match(self.original):
if PLAIN.match(self.original):
return True
elif hyphen.match(self.original):
elif HYPHEN.match(self.original):
return True
elif colon.match(self.original):
elif COLON.match(self.original):
return True
elif dot.match(self.original):
elif DOT.match(self.original):
return True
else:
return False

@property
def normalized(self):
pattern = re.compile("[^0-9A-Fa-f]")
return pattern.sub("", self.original.lower())
return NOT_DIGITS.sub("", self.original.lower())

@property
def octets(self):
# Create one instance of Octet for each of the
# hexadecimal identifier's six octets.

pattern = re.compile("[0-9a-f]{2}")
matches = pattern.findall(self.normalized)
matches = TWO_DIGITS.findall(self.normalized)
return [Octet(match) for match in matches]

@property
Expand Down Expand Up @@ -254,8 +256,7 @@ def to_hyphen_notation(self):
(for example, `a0-b1-c2-d3-e4-f5`).
"""

pattern = re.compile("[0-9a-f]{2}")
matches = pattern.findall(self.normalized)
matches = TWO_DIGITS.findall(self.normalized)
return "-".join(matches)

def to_colon_notation(self):
Expand All @@ -264,8 +265,7 @@ def to_colon_notation(self):
(for example, `a0:b1:c2:d3:e4:f5`).
"""

pattern = re.compile("[0-9a-f]{2}")
matches = pattern.findall(self.normalized)
matches = TWO_DIGITS.findall(self.normalized)
return ":".join(matches)

def to_dot_notation(self):
Expand All @@ -274,6 +274,5 @@ def to_dot_notation(self):
(for example, `a0b1.c2d3.e4f5`).
"""

pattern = re.compile("[0-9a-f]{4}")
matches = pattern.findall(self.normalized)
matches = FOUR_DIGITS.findall(self.normalized)
return ".".join(matches)
7 changes: 4 additions & 3 deletions macaddress/octet.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import re


DIGITS = re.compile("^[0-9A-Fa-f]{2}$")


class OctetError(Exception):
"""
Octet raises OctetError if instantiated with an invalid argument.
Expand Down Expand Up @@ -84,9 +87,7 @@ def __str__(self):
def is_valid(self):
# Evaluate the hexadecimal digits.

pattern = re.compile("^[0-9A-Fa-f]{2}$")

if pattern.match(self.original):
if DIGITS.match(self.original):
return True
else:
return False
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name="macaddress",
version="0.7.0",
version="0.8.0",
description="The macaddress library makes it easy to work with media access control (MAC) addresses.",
url="https://github.com/critical-path/macaddress",
author="critical-path",
Expand Down

0 comments on commit 73bc213

Please sign in to comment.