This repository has been archived by the owner on Jan 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
0d3be1b
commit 5331b1c
Showing
3 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import re | ||
from types import SimpleNamespace | ||
from .util import validate_regexp, weighted_modulus_digit, modulus_overflow_mod10 | ||
|
||
|
||
class OldIdentityCard: | ||
""" | ||
Greece Identity Card, the old one. | ||
https://en.wikipedia.org/wiki/National_identification_number#Greece | ||
""" | ||
METADATA = SimpleNamespace(**{ | ||
'iso3166_alpha2': 'GR', | ||
'min_length': 7, | ||
'max_length': 7, | ||
'parsable': False, | ||
'checksum': False, | ||
'regexp': re.compile(r'^[ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ]-?\d{6}$') | ||
}) | ||
|
||
@staticmethod | ||
def validate(id_number: str) -> bool: | ||
""" | ||
Validate with regexp | ||
""" | ||
return validate_regexp(id_number, OldIdentityCard.METADATA.regexp) | ||
|
||
|
||
class IdentityCard: | ||
""" | ||
Greece Identity Card, the new one. | ||
https://en.wikipedia.org/wiki/National_identification_number#Greece | ||
""" | ||
METADATA = SimpleNamespace(**{ | ||
'iso3166_alpha2': 'GR', | ||
'min_length': 7, | ||
'max_length': 7, | ||
'parsable': False, | ||
'checksum': False, | ||
'regexp': re.compile(r'^[ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩABEZHIKMNOPTYX]{2}-?\d{6}$') | ||
# They are two different char set, the former is Greek alphabet, the latter is Latin alphabet | ||
}) | ||
|
||
@staticmethod | ||
def validate(id_number: str) -> bool: | ||
""" | ||
Validate with regexp | ||
""" | ||
return validate_regexp(id_number, IdentityCard.METADATA.regexp) | ||
|
||
|
||
class TaxIdentityNumber: | ||
""" | ||
Greece Tax Identity Number, AFM - ΑΦΜ - Αριθμός Φορολογικού Μητρώου - Tax Registry Number | ||
https://en.wikipedia.org/wiki/National_identification_number#Greece | ||
""" | ||
METADATA = SimpleNamespace(**{ | ||
'iso3166_alpha2': 'GR', | ||
'min_length': 9, | ||
'max_length': 9, | ||
'parsable': False, | ||
'checksum': True, | ||
'regexp': re.compile(r'^\d{9}$') | ||
}) | ||
|
||
MULTIPLIER = [256, 128, 64, 32, 16, 8, 4, 2] | ||
|
||
@staticmethod | ||
def validate(id_number: str) -> bool: | ||
""" | ||
Validate with regexp | ||
""" | ||
return TaxIdentityNumber.checksum(id_number) | ||
|
||
@staticmethod | ||
def checksum(id_number: str) -> bool: | ||
""" | ||
ref: https://stackoverflow.com/a/4377376 | ||
""" | ||
if not validate_regexp(id_number, TaxIdentityNumber.METADATA.regexp): | ||
return False | ||
numbers = [int(char) for char in id_number] | ||
modulus = modulus_overflow_mod10(weighted_modulus_digit(numbers[0:-1], TaxIdentityNumber.MULTIPLIER, 11, True)) | ||
return numbers[-1] == modulus | ||
|
||
|
||
NationalID = IdentityCard | ||
""" | ||
alias of IdentityCard | ||
""" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from unittest import TestCase | ||
|
||
from idnumbers.nationalid import GRC | ||
|
||
|
||
class TestGRCNationalInsuranceNumberValidation(TestCase): | ||
def test_normal_case(self): | ||
self.assertTrue(GRC.OldIdentityCard.validate('Φ-123456')) | ||
self.assertTrue(GRC.IdentityCard.validate('ΦA-123456')) | ||
self.assertTrue(GRC.TaxIdentityNumber.validate('355827182')) | ||
|
||
def test_error_case(self): | ||
self.assertFalse(GRC.OldIdentityCard.validate('A-123456')) # test with latin | ||
self.assertFalse(GRC.IdentityCard.validate('A-123456')) # wrong digits | ||
self.assertFalse(GRC.TaxIdentityNumber.validate('355827181')) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from unittest import TestCase, main | ||
from unittest import TestCase | ||
|
||
from idnumbers.nationalid import HUN | ||
|
||
|