Created
August 31, 2025 13:33
-
-
Save zvr/87d07e98e70edb0aedea8ebe429945ad 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
| # code utilities for Greek identifiers (ΑΦΜ and ΠΑ) | |
| # Copyright (C) 2025 Alexios Zavras | |
| # SPDX-License-Identifier: GPL-3.0-or-later | |
| class ΑΦΜ: | |
| VALID_CHARS = set("0123456789") | |
| def __init__(self, value: str): | |
| self.value = value | |
| def __str__(self): | |
| return self.value | |
| def is_valid(self) -> bool: | |
| if len(self.value) != 9: | |
| return False | |
| if not all(c in self.VALID_CHARS for c in self.value): | |
| return False | |
| total = sum(int(self.value[8-i])*(1<<i) for i in range(1,9)) | |
| check_digit = (total % 11) % 10 | |
| return check_digit == int(self.value[8]) | |
| class ΠΑ: | |
| ALPHABET = "0123456789ΑΒΕΖΗΙΚΜΝΟΡΤΥΧ" | |
| def __init__(self, value: str): | |
| self.value = value | |
| def __str__(self): | |
| return self.value | |
| def is_valid(self, *, allow_latin:bool = False) -> bool: | |
| if len(self.value) != 12: | |
| return False | |
| if not ΑΦΜ(self.value[3:]).is_valid(): | |
| return False | |
| s = self.value | |
| if allow_latin: | |
| s = s.translate(str.maketrans("ABEZHIKMNOPTYX", "ΑΒΕΖΗΙΚΜΝΟΡΤΥΧ")) | |
| if not all(c in self.ALPHABET for c in s): | |
| return False | |
| c2n = {c: i for i, c in enumerate(self.ALPHABET)} | |
| total = sum((c2n[s[i]] * (1 + i%2)) % 23 for i in range(12)) | |
| return (total % 14) == 10 | |
| def gen_all_pa(afm: str, *, prefixes_only: bool = False): | |
| if not ΑΦΜ(afm).is_valid(): | |
| raise ValueError("Invalid ΑΦΜ") | |
| c = sum(int(afm[i]) + (0 if i%2 else int(afm[i])) for i in range(9)) | |
| for a, la in enumerate(ΠΑ.ALPHABET): | |
| for b, lb in enumerate(ΠΑ.ALPHABET): | |
| t = a + ((2*b) % 23) + c | |
| x = 10 + (-t % 14) | |
| lx = ΠΑ.ALPHABET[x] | |
| yield f"{la}{lb}{lx}" + ("" if prefixes_only else afm) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment