Last active
September 4, 2025 15:39
-
-
Save koi8-r/8929e6090af55a5325d078dcdf6b1431 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import TypeAlias, TypeVar | |
| T = TypeVar("T") | |
| Tuple6: TypeAlias = tuple[T, T, T, T, T, T] | |
| OCTET_MAX = 0xFF | |
| def parse(octets: Tuple6[str]) -> int: # like: int(str, 16) | |
| off = ret = 0 | |
| for ch in reversed(octets): | |
| try: | |
| o = int(ch, 16) | |
| if not 0 <= o <= OCTET_MAX: | |
| raise ValueError | |
| except ValueError: | |
| raise ValueError("not a valid hex octet", ch) from None | |
| ret = ret | (o << off) | |
| off += 4 * len(ch) | |
| return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment