Repository: identique/idnumbers (language: python)

Issue:
id number for Turkey
https://en.wikipedia.org/wiki/National_identification_number#Turkey


Discussion / hints:

Fix patch:
diff --git a/idnumbers/nationalid/TUR.py b/idnumbers/nationalid/TUR.py
new file mode 100644
index 00000000..f9db1f83
--- /dev/null
+++ b/idnumbers/nationalid/TUR.py
@@ -0,0 +1,41 @@
+import re
+from types import SimpleNamespace
+from .util import validate_regexp, weighted_modulus_digit
+
+
+class NationalID:
+    """
+    Turkey National ID number
+    https://en.wikipedia.org/wiki/National_identification_number#Turkey
+    """
+    METADATA = SimpleNamespace(**{
+        'iso3166_alpha2': 'TR',
+        # length without insignificant chars
+        'min_length': 11,
+        'max_length': 11,
+        'parsable': False,
+        'checksum': True,
+        'regexp': re.compile(r'^[1-9]\d{10}$')
+    })
+
+    @staticmethod
+    def validate(id_number: str) -> bool:
+        """
+        Validate TUR national id number
+        """
+        if not validate_regexp(id_number, NationalID.METADATA.regexp):
+            return False
+        return NationalID.checksum(id_number) == id_number[-2:]
+
+    @staticmethod
+    def checksum(id_number: str) -> str:
+        """
+        https://stackoverflow.com/questions/53610208/turkish-identity-number-verification
+        Calculate the checksum (the last two digits)
+        """
+        MOD = 10
+        MULTIPLIERS = [7, -1, 7, -1, 7, -1, 7, -1, 7]
+        numbers_list = [int(i) for i in id_number]
+        digit_ten = weighted_modulus_digit(numbers_list[:-2], MULTIPLIERS, MOD, True)
+        digit_eleven = sum(numbers_list[:-1]) % MOD
+        return f'{digit_ten}{digit_eleven}'


Test patch:
diff --git a/idnumbers/nationalid/test_TUR.py b/idnumbers/nationalid/test_TUR.py
new file mode 100644
index 00000000..478d0ba0
--- /dev/null
+++ b/idnumbers/nationalid/test_TUR.py
@@ -0,0 +1,17 @@
+from unittest import TestCase, main
+
+from idnumbers.nationalid import TUR
+
+
+class TestTURValidation(TestCase):
+    def test_normal_case(self):
+        self.assertTrue(TUR.NationalID.validate('10000000146'))
+        self.assertTrue(TUR.NationalID.validate('15973515680'))
+
+    def test_error_case(self):
+        self.assertFalse(TUR.NationalID.validate('00000000178'))
+        self.assertFalse(TUR.NationalID.validate('10000000145'))
+
+
+if __name__ == '__main__':
+    main()