niftools 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
niftools-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Pablo Castelo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ recursive-include tests *.py
@@ -0,0 +1,94 @@
1
+ Metadata-Version: 2.4
2
+ Name: niftools
3
+ Version: 0.1.0
4
+ Summary: Validador mínimo y sin dependencias de DNI, NIE y NIF de entidades
5
+ Author: Pablo Castelo
6
+ License: MIT License
7
+
8
+ Copyright (c) 2026 Pablo Castelo
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Homepage, https://github.com/pcastelovigo/validarnif
29
+ Project-URL: Repository, https://github.com/pcastelovigo/validarnif
30
+ Project-URL: Issues, https://github.com/pcastelovigo/validarnif/issues
31
+ Keywords: nif,nie,cif,dni,spain,validation
32
+ Classifier: Development Status :: 4 - Beta
33
+ Classifier: Intended Audience :: Developers
34
+ Classifier: License :: OSI Approved :: MIT License
35
+ Classifier: Operating System :: OS Independent
36
+ Classifier: Programming Language :: Python :: 3
37
+ Classifier: Programming Language :: Python :: 3 :: Only
38
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
39
+ Requires-Python: >=3.8
40
+ Description-Content-Type: text/markdown
41
+ License-File: LICENSE
42
+ Dynamic: license-file
43
+
44
+ # niftools
45
+
46
+ Validador mínimo y sin dependencias de identificadores fiscales españoles.
47
+ Comprueba DNI, NIE y NIF de entidades (tradicionalmente llamado CIF) mediante
48
+ su formato y carácter de control.
49
+
50
+ ## Instalación
51
+
52
+ ```bash
53
+ pip install niftools
54
+ ```
55
+
56
+ ## Uso
57
+
58
+ ```python
59
+ from niftools import normalizarnif, validarnif
60
+
61
+ nif = normalizarnif("ES A-1234567-4") # "A12345674"
62
+ validarnif(nif) # True
63
+
64
+ validarnif("12345678Z") # True
65
+ validarnif("X-1234567-L") # True
66
+ validarnif("12345678A") # False
67
+ ```
68
+
69
+ `normalizarnif()` elimina puntos, guiones, espacios y el prefijo internacional
70
+ opcional `ES`, y convierte el texto a mayúsculas. `validarnif()` reutiliza esa
71
+ normalización, por lo que acepta tanto identificadores normalizados como
72
+ identificadores con separadores.
73
+
74
+ La validación comprueba la estructura y el carácter de control. No confirma
75
+ que el identificador haya sido emitido ni que pertenezca a una persona o
76
+ entidad determinada.
77
+
78
+ El alcance incluye DNI, NIE y NIF de entidades (tradicionalmente CIF). No
79
+ incluye los NIF personales especiales que comienzan por `K`, `L` o `M` y
80
+ contienen un cuerpo alfanumérico.
81
+
82
+ ## Compatibilidad
83
+
84
+ Python 3.8 o posterior.
85
+
86
+ ## Pruebas
87
+
88
+ ```bash
89
+ python -m unittest discover -v
90
+ ```
91
+
92
+ ## Licencia
93
+
94
+ MIT.
@@ -0,0 +1,51 @@
1
+ # niftools
2
+
3
+ Validador mínimo y sin dependencias de identificadores fiscales españoles.
4
+ Comprueba DNI, NIE y NIF de entidades (tradicionalmente llamado CIF) mediante
5
+ su formato y carácter de control.
6
+
7
+ ## Instalación
8
+
9
+ ```bash
10
+ pip install niftools
11
+ ```
12
+
13
+ ## Uso
14
+
15
+ ```python
16
+ from niftools import normalizarnif, validarnif
17
+
18
+ nif = normalizarnif("ES A-1234567-4") # "A12345674"
19
+ validarnif(nif) # True
20
+
21
+ validarnif("12345678Z") # True
22
+ validarnif("X-1234567-L") # True
23
+ validarnif("12345678A") # False
24
+ ```
25
+
26
+ `normalizarnif()` elimina puntos, guiones, espacios y el prefijo internacional
27
+ opcional `ES`, y convierte el texto a mayúsculas. `validarnif()` reutiliza esa
28
+ normalización, por lo que acepta tanto identificadores normalizados como
29
+ identificadores con separadores.
30
+
31
+ La validación comprueba la estructura y el carácter de control. No confirma
32
+ que el identificador haya sido emitido ni que pertenezca a una persona o
33
+ entidad determinada.
34
+
35
+ El alcance incluye DNI, NIE y NIF de entidades (tradicionalmente CIF). No
36
+ incluye los NIF personales especiales que comienzan por `K`, `L` o `M` y
37
+ contienen un cuerpo alfanumérico.
38
+
39
+ ## Compatibilidad
40
+
41
+ Python 3.8 o posterior.
42
+
43
+ ## Pruebas
44
+
45
+ ```bash
46
+ python -m unittest discover -v
47
+ ```
48
+
49
+ ## Licencia
50
+
51
+ MIT.
@@ -0,0 +1,94 @@
1
+ Metadata-Version: 2.4
2
+ Name: niftools
3
+ Version: 0.1.0
4
+ Summary: Validador mínimo y sin dependencias de DNI, NIE y NIF de entidades
5
+ Author: Pablo Castelo
6
+ License: MIT License
7
+
8
+ Copyright (c) 2026 Pablo Castelo
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Homepage, https://github.com/pcastelovigo/validarnif
29
+ Project-URL: Repository, https://github.com/pcastelovigo/validarnif
30
+ Project-URL: Issues, https://github.com/pcastelovigo/validarnif/issues
31
+ Keywords: nif,nie,cif,dni,spain,validation
32
+ Classifier: Development Status :: 4 - Beta
33
+ Classifier: Intended Audience :: Developers
34
+ Classifier: License :: OSI Approved :: MIT License
35
+ Classifier: Operating System :: OS Independent
36
+ Classifier: Programming Language :: Python :: 3
37
+ Classifier: Programming Language :: Python :: 3 :: Only
38
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
39
+ Requires-Python: >=3.8
40
+ Description-Content-Type: text/markdown
41
+ License-File: LICENSE
42
+ Dynamic: license-file
43
+
44
+ # niftools
45
+
46
+ Validador mínimo y sin dependencias de identificadores fiscales españoles.
47
+ Comprueba DNI, NIE y NIF de entidades (tradicionalmente llamado CIF) mediante
48
+ su formato y carácter de control.
49
+
50
+ ## Instalación
51
+
52
+ ```bash
53
+ pip install niftools
54
+ ```
55
+
56
+ ## Uso
57
+
58
+ ```python
59
+ from niftools import normalizarnif, validarnif
60
+
61
+ nif = normalizarnif("ES A-1234567-4") # "A12345674"
62
+ validarnif(nif) # True
63
+
64
+ validarnif("12345678Z") # True
65
+ validarnif("X-1234567-L") # True
66
+ validarnif("12345678A") # False
67
+ ```
68
+
69
+ `normalizarnif()` elimina puntos, guiones, espacios y el prefijo internacional
70
+ opcional `ES`, y convierte el texto a mayúsculas. `validarnif()` reutiliza esa
71
+ normalización, por lo que acepta tanto identificadores normalizados como
72
+ identificadores con separadores.
73
+
74
+ La validación comprueba la estructura y el carácter de control. No confirma
75
+ que el identificador haya sido emitido ni que pertenezca a una persona o
76
+ entidad determinada.
77
+
78
+ El alcance incluye DNI, NIE y NIF de entidades (tradicionalmente CIF). No
79
+ incluye los NIF personales especiales que comienzan por `K`, `L` o `M` y
80
+ contienen un cuerpo alfanumérico.
81
+
82
+ ## Compatibilidad
83
+
84
+ Python 3.8 o posterior.
85
+
86
+ ## Pruebas
87
+
88
+ ```bash
89
+ python -m unittest discover -v
90
+ ```
91
+
92
+ ## Licencia
93
+
94
+ MIT.
@@ -0,0 +1,11 @@
1
+ LICENSE
2
+ MANIFEST.in
3
+ README.md
4
+ niftools.py
5
+ pyproject.toml
6
+ niftools.egg-info/PKG-INFO
7
+ niftools.egg-info/SOURCES.txt
8
+ niftools.egg-info/dependency_links.txt
9
+ niftools.egg-info/top_level.txt
10
+ tests/__init__.py
11
+ tests/test_validarnif.py
@@ -0,0 +1 @@
1
+ niftools
@@ -0,0 +1,15 @@
1
+ def normalizarnif(nif: str) -> str:
2
+ nif=nif.upper().translate(str.maketrans("", "", ".- "))
3
+ return nif[2:] if nif.startswith("ES") else nif
4
+
5
+
6
+ def validarnif(nif: str) -> bool:
7
+ nif=normalizarnif(nif)
8
+ if len(nif) != 9 or not nif.isascii() or not nif[1:8].isdigit(): return False
9
+ pr, nu, ct = nif[0],nif[1:8],nif[8]
10
+ lt = "TRWAGMYFPDXBNJZSQVHLCKE"
11
+ if pr.isdigit():return ct==lt[int(nif[:8]) % 23]
12
+ if pr in"XYZ":return ct==lt[(int(nu)+"XYZ".index(pr)*10000000)%23]
13
+ tt=sum(map(int,nu[1::2]))+sum(2*int(cf)-9*(cf>"4")for cf in nu[::2])
14
+ dg=-tt%10
15
+ return pr in"ABCDEFGHJNPQRSUVW"and ct in(str(dg),"JABCDEFGHI"[dg])
@@ -0,0 +1,30 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "niftools"
7
+ version = "0.1.0"
8
+ description = "Validador mínimo y sin dependencias de DNI, NIE y NIF de entidades"
9
+ authors = [{name = "Pablo Castelo"}]
10
+ readme = "README.md"
11
+ requires-python = ">=3.8"
12
+ license = {file = "LICENSE"}
13
+ keywords = ["nif", "nie", "cif", "dni", "spain", "validation"]
14
+ classifiers = [
15
+ "Development Status :: 4 - Beta",
16
+ "Intended Audience :: Developers",
17
+ "License :: OSI Approved :: MIT License",
18
+ "Operating System :: OS Independent",
19
+ "Programming Language :: Python :: 3",
20
+ "Programming Language :: Python :: 3 :: Only",
21
+ "Topic :: Software Development :: Libraries :: Python Modules",
22
+ ]
23
+
24
+ [project.urls]
25
+ Homepage = "https://github.com/pcastelovigo/validarnif"
26
+ Repository = "https://github.com/pcastelovigo/validarnif"
27
+ Issues = "https://github.com/pcastelovigo/validarnif/issues"
28
+
29
+ [tool.setuptools]
30
+ py-modules = ["niftools"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
File without changes
@@ -0,0 +1,75 @@
1
+ import unittest
2
+
3
+ from niftools import normalizarnif, validarnif
4
+
5
+
6
+ class ValidarNifTests(unittest.TestCase):
7
+ def test_normalizar_nif(self):
8
+ self.assertEqual(normalizarnif("A-1234567-4"), "A12345674")
9
+ self.assertEqual(normalizarnif("12.345.678-z"), "12345678Z")
10
+ self.assertEqual(normalizarnif("ES A-1234567-4"), "A12345674")
11
+ self.assertEqual(normalizarnif("es12.345.678-z"), "12345678Z")
12
+
13
+ def test_validar_salida_normalizada(self):
14
+ nif = normalizarnif("X-1234567-L")
15
+ self.assertTrue(validarnif(nif))
16
+
17
+ def test_prefijo_es(self):
18
+ self.assertTrue(validarnif("ES A-1234567-4"))
19
+ self.assertTrue(validarnif("es12.345.678-z"))
20
+
21
+ def test_rechaza_digitos_no_ascii(self):
22
+ self.assertFalse(validarnif("A١٢٣٤٥٦٧4"))
23
+
24
+ def test_dni(self):
25
+ self.assertTrue(validarnif("12345678Z"))
26
+ self.assertFalse(validarnif("12345678A"))
27
+
28
+ def test_nie(self):
29
+ self.assertTrue(validarnif("X1234567L"))
30
+ self.assertFalse(validarnif("X1234567A"))
31
+
32
+ def test_cif_con_control_numerico_o_alfabetico(self):
33
+ self.assertTrue(validarnif("A12345674"))
34
+ self.assertTrue(validarnif("A1234567D"))
35
+
36
+ def test_control_cif_cero(self):
37
+ self.assertTrue(validarnif("A00000000"))
38
+ self.assertTrue(validarnif("A0000000J"))
39
+
40
+ def test_normalizacion(self):
41
+ self.assertTrue(validarnif("12.345.678-z"))
42
+ self.assertTrue(validarnif("X-1234567-L"))
43
+ self.assertTrue(validarnif("A 123 456 74"))
44
+
45
+ def test_formatos_invalidos(self):
46
+ self.assertFalse(validarnif(""))
47
+ self.assertFalse(validarnif("O12345674"))
48
+ self.assertFalse(validarnif("A12345675"))
49
+ self.assertFalse(validarnif("1234A678Z"))
50
+
51
+ def test_38000_combinaciones_validas(self):
52
+ letras = "TRWAGMYFPDXBNJZSQVHLCKE"
53
+ controles = "JABCDEFGHI"
54
+ prefijos = "ABCDEFGHJNPQRSUVW"
55
+
56
+ for numero in range(1000):
57
+ cuerpo = f"{numero:07d}"
58
+ total = sum(map(int, cuerpo[1::2]))
59
+ total += sum(
60
+ sum(divmod(2 * int(cifra), 10)) for cifra in cuerpo[::2]
61
+ )
62
+ control = -total % 10
63
+ for prefijo in prefijos:
64
+ self.assertTrue(validarnif(prefijo + cuerpo + str(control)))
65
+ self.assertTrue(validarnif(prefijo + cuerpo + controles[control]))
66
+
67
+ dni = f"{numero:08d}"
68
+ self.assertTrue(validarnif(dni + letras[numero % 23]))
69
+ for prefijo, valor in zip("XYZ", range(3)):
70
+ posicion = (valor * 10000000 + numero) % 23
71
+ self.assertTrue(validarnif(prefijo + cuerpo + letras[posicion]))
72
+
73
+
74
+ if __name__ == "__main__":
75
+ unittest.main()