diff options
| author | Bobby <[email protected]> | 2022-11-27 16:14:33 -0500 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-11-27 16:14:33 -0500 |
| commit | b245307f903d8eee53e880c3d68b77ee32d07003 (patch) | |
| tree | 2153488f311fcc9f34282161380e6cfacd2d8b9f | |
| parent | 1ed7e8bac832d4f8de8a320d581372038f7011d4 (diff) | |
| download | edify-b245307f903d8eee53e880c3d68b77ee32d07003.tar.xz edify-b245307f903d8eee53e880c3d68b77ee32d07003.zip | |
added ssn and mac validators
| -rw-r--r-- | docs/built-in/index.rst | 30 | ||||
| -rw-r--r-- | src/edify/library/__init__.py | 2 | ||||
| -rw-r--r-- | src/edify/library/mac.py | 16 | ||||
| -rw-r--r-- | src/edify/library/ssn.py | 16 | ||||
| -rw-r--r-- | tests/ssn_test.py | 11 | ||||
| -rw-r--r-- | tests/test_mac.py | 11 |
6 files changed, 86 insertions, 0 deletions
diff --git a/docs/built-in/index.rst b/docs/built-in/index.rst index d63d4c2..8887b5c 100644 --- a/docs/built-in/index.rst +++ b/docs/built-in/index.rst @@ -989,3 +989,33 @@ Here's an example of how to use the ``password`` function: password("password", min_upper=0, min_digit=0, min_special=0) # returns True password("pass@#1", min_special=1, special_chars="!", min_digit=0, min_upper=0, min_length=4) # returns False +ssn(ssn: str) +------------- + +The ``ssn`` function validates a Social Security Number (SSN) string. The function returns ``True`` if the string is a valid SSN, and ``False`` otherwise. + +Here's an example of how to use the ``ssn`` function: + +.. code-block:: python + + from edify.library import ssn + + ssn('123-45-6789') # returns True + ssn('123-45-678') # returns False + ssn('123-45-67890') # returns False + + +mac(mac: str) +------------- + +The ``mac`` function validates a MAC address (IEEE 802) string. The function returns ``True`` if the string is a valid MAC address, and ``False`` otherwise. + +Here's an example of how to use the ``mac`` function: + +.. code-block:: python + + from edify.library import mac + + mac('00:00:00:00:00:00') # returns True + mac('00:00:00:00:00:0') # returns False + mac('00:00:00:00:00:000') # returns False diff --git a/src/edify/library/__init__.py b/src/edify/library/__init__.py index 62d6528..5f091e8 100644 --- a/src/edify/library/__init__.py +++ b/src/edify/library/__init__.py @@ -13,3 +13,5 @@ from .uuid import uuid from .zip import zip from .guid import guid from .password import password +from .ssn import ssn +from .mac import mac diff --git a/src/edify/library/mac.py b/src/edify/library/mac.py new file mode 100644 index 0000000..9a8cf8f --- /dev/null +++ b/src/edify/library/mac.py @@ -0,0 +1,16 @@ +import re + +mac_address_validate_pattern = "^(?:[0-9A-Fa-f]{2}[:-]){5}(?:[0-9A-Fa-f]{2})$" + +def mac(mac: str) -> bool: + """Validate a MAC (IEEE 802) address. + + Args: + mac (str): The MAC address to validate. + + Returns: + bool: True if the MAC address is valid, False otherwise. + """ + if not isinstance(mac, str): + return False + return bool(re.match(mac_address_validate_pattern, mac)) diff --git a/src/edify/library/ssn.py b/src/edify/library/ssn.py new file mode 100644 index 0000000..8f42de6 --- /dev/null +++ b/src/edify/library/ssn.py @@ -0,0 +1,16 @@ +import re + +ssn_validate_pattern = "^(?!666|000|9\\d{2})\\d{3}-(?!00)\\d{2}-(?!0{4})\\d{4}$" + +def ssn(ssn: str) -> bool: + """Validate a Social Security Number (SSN). + + Args: + ssn (str): The SSN to validate. + + Returns: + bool: True if the SSN is valid, False otherwise. + """ + if not isinstance(ssn, str): + return False + return bool(re.match(ssn_validate_pattern, ssn)) diff --git a/tests/ssn_test.py b/tests/ssn_test.py new file mode 100644 index 0000000..8317ec0 --- /dev/null +++ b/tests/ssn_test.py @@ -0,0 +1,11 @@ +from edify.library import ssn + +def test_ssn(): + ssns = { + "000-22-3333": False, + "100-22-3333": True, + "": False, + 123: False, + } + for s_s_n, expected in ssns.items(): + assert ssn(s_s_n) == expected diff --git a/tests/test_mac.py b/tests/test_mac.py new file mode 100644 index 0000000..636b649 --- /dev/null +++ b/tests/test_mac.py @@ -0,0 +1,11 @@ +from edify.library import mac + +def test_mac(): + macs = { + "00:00:5e:00:53:af": True, + "00:00:5e:00:53:af:": False, + 123: False, + } + + for m_a_c, expected in macs.items(): + assert mac(m_a_c) == expected |
