cpf-alfacnpj 0.2.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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Andrey Sant'Anna
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,16 @@
1
+ Metadata-Version: 2.1
2
+ Name: cpf_alfacnpj
3
+ Version: 0.2.0
4
+ Summary: Biblioteca de validação de CPF e CNPJ, inclusive com suporte ao CNPJ alfanumérico (2026)
5
+ Home-page: https://github.com/andreydani/cpf_cnpj
6
+ Author: Andrey Sant'Anna
7
+ Author-email: andreysantanna@gmail.com
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.6
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+
15
+ # cpf_alfacnpj
16
+ Biblioteca de validação de CPF e CNPJ, inclusive com suporte ao CNPJ alfanumérico (2026)
@@ -0,0 +1,2 @@
1
+ # cpf_alfacnpj
2
+ Biblioteca de validação de CPF e CNPJ, inclusive com suporte ao CNPJ alfanumérico (2026)
@@ -0,0 +1,2 @@
1
+ from .cpf import validar_cpf # NOQA
2
+ from .cnpj import validar_cnpj, validar_cnpj_2026 # NOQA
@@ -0,0 +1,67 @@
1
+ from typing import Callable
2
+
3
+
4
+ def calcular_dv1(cnpj: str) -> int:
5
+ """
6
+ Calculo do dígito verificdor 1, já preparado
7
+ para lidar com CNPJ alfanumérico
8
+ :param cnpj:
9
+ :return:
10
+ """
11
+ pesos1: list[int] = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
12
+ soma: int = sum((ord(cnpj[i]) - 48) * pesos1[i] for i in range(12))
13
+ dv1: int = 11 - (soma % 11)
14
+ return 0 if dv1 >= 10 else dv1
15
+
16
+
17
+ def calcular_dv2(cnpj: str) -> int:
18
+ """
19
+ Calculo do dígito verificdor 1, já preparado
20
+ para lidar com CNPJ alfanumérico
21
+ :param cnpj:
22
+ :return:
23
+ """
24
+ pesos2: list[int] = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
25
+ soma = sum((ord(cnpj[i]) - 48) * pesos2[i] for i in range(13))
26
+ dv2: int = 11 - (soma % 11)
27
+ return 0 if dv2 >= 10 else dv2
28
+
29
+
30
+ def validar_cnpj_base(cnpj: str, limpa: Callable[[str], str]) -> bool:
31
+ # Remove caracteres não numéricos
32
+ cnpj: str = limpa(cnpj)
33
+
34
+ # Verifica se o CNPJ tem 14 caracteres
35
+ if len(cnpj) != 14:
36
+ return False
37
+
38
+ # Verifica se todos os caracteres são iguais
39
+ if cnpj == cnpj[0] * 14:
40
+ return False
41
+
42
+ # Validação do primeiro dígito verificador
43
+ dv1: int = calcular_dv1(cnpj)
44
+ if int(cnpj[12]) != dv1:
45
+ return False
46
+
47
+ # Validação do segundo dígito verificador
48
+ dv2: int = calcular_dv2(cnpj)
49
+ if int(cnpj[13]) != dv2:
50
+ return False
51
+
52
+ return True
53
+
54
+
55
+ def validar_cnpj(cnpj: str) -> bool:
56
+ return validar_cnpj_base(
57
+ cnpj,
58
+ lambda cnpj: ''.join(filter(str.isdigit, cnpj))
59
+ )
60
+
61
+
62
+ def validar_cnpj_2026(cnpj: str) -> bool:
63
+ return validar_cnpj_base(
64
+ cnpj,
65
+ lambda cnpj: ''.join(filter(str.isalnum, cnpj[:-2])).upper() +
66
+ ''.join(filter(str.isdigit, cnpj[-2:]))
67
+ )
@@ -0,0 +1,33 @@
1
+ def calcular_dv1(cpf: str) -> int:
2
+ soma: int = sum(int(cpf[i]) * (10 - i) for i in range(9))
3
+ return (soma * 10 % 11) % 10
4
+
5
+
6
+ def calcular_dv2(cpf: str) -> int:
7
+ soma: int = sum(int(cpf[i]) * (11 - i) for i in range(10))
8
+ return (soma * 10 % 11) % 10
9
+
10
+
11
+ def validar_cpf(cpf: str) -> bool:
12
+ # Remove caracteres não numéricos
13
+ cpf = ''.join(filter(str.isdigit, cpf))
14
+
15
+ # Verifica se o CPF tem 11 dígitos
16
+ if len(cpf) != 11:
17
+ return False
18
+
19
+ # Verifica se todos os dígitos são iguais
20
+ if cpf == cpf[0] * 11:
21
+ return False
22
+
23
+ # Validação do primeiro dígito verificador
24
+ dv1: int = calcular_dv1(cpf)
25
+ if int(cpf[9]) != dv1:
26
+ return False
27
+
28
+ # Validação do segundo dígito verificador
29
+ dv2: int = calcular_dv2(cpf)
30
+ if int(cpf[10]) != dv2:
31
+ return False
32
+
33
+ return True
@@ -0,0 +1,16 @@
1
+ Metadata-Version: 2.1
2
+ Name: cpf_alfacnpj
3
+ Version: 0.2.0
4
+ Summary: Biblioteca de validação de CPF e CNPJ, inclusive com suporte ao CNPJ alfanumérico (2026)
5
+ Home-page: https://github.com/andreydani/cpf_cnpj
6
+ Author: Andrey Sant'Anna
7
+ Author-email: andreysantanna@gmail.com
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.6
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+
15
+ # cpf_alfacnpj
16
+ Biblioteca de validação de CPF e CNPJ, inclusive com suporte ao CNPJ alfanumérico (2026)
@@ -0,0 +1,15 @@
1
+ LICENSE
2
+ README.md
3
+ setup.py
4
+ cpf_alfacnpj/__init__.py
5
+ cpf_alfacnpj/cnpj.py
6
+ cpf_alfacnpj/cpf.py
7
+ cpf_alfacnpj.egg-info/PKG-INFO
8
+ cpf_alfacnpj.egg-info/SOURCES.txt
9
+ cpf_alfacnpj.egg-info/dependency_links.txt
10
+ cpf_alfacnpj.egg-info/entry_points.txt
11
+ cpf_alfacnpj.egg-info/top_level.txt
12
+ tests/__init__.py
13
+ tests/test_validar_cnpj.py
14
+ tests/test_validar_cnpj_2026.py
15
+ tests/test_validar_cpf.py
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ cpf-alfacnpj = cpf_alfacnpj.main:main
@@ -0,0 +1,2 @@
1
+ cpf_alfacnpj
2
+ tests
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,26 @@
1
+ # setup.py
2
+ from setuptools import setup, find_packages
3
+
4
+ setup(
5
+ name="cpf_alfacnpj",
6
+ version="0.2.0",
7
+ packages=find_packages(),
8
+ install_requires=[],
9
+ entry_points={
10
+ 'console_scripts': [
11
+ 'cpf-alfacnpj=cpf_alfacnpj.main:main',
12
+ ],
13
+ },
14
+ author="Andrey Sant'Anna",
15
+ author_email="andreysantanna@gmail.com",
16
+ description="Biblioteca de validação de CPF e CNPJ, inclusive com suporte ao CNPJ alfanumérico (2026)",
17
+ long_description=open('README.md').read(),
18
+ long_description_content_type="text/markdown",
19
+ url="https://github.com/andreydani/cpf_cnpj",
20
+ classifiers=[
21
+ "Programming Language :: Python :: 3",
22
+ "License :: OSI Approved :: MIT License",
23
+ "Operating System :: OS Independent",
24
+ ],
25
+ python_requires='>=3.6',
26
+ )
File without changes
@@ -0,0 +1,27 @@
1
+ import unittest
2
+ from cpf_alfacnpj import validar_cnpj
3
+
4
+
5
+ class TestValidarCNPJ(unittest.TestCase):
6
+
7
+ def test_cnpj_valido(self):
8
+ self.assertTrue(validar_cnpj("04.252.011/0001-10"))
9
+ self.assertTrue(validar_cnpj("12.345.678/0001-95"))
10
+
11
+ def test_cnpj_invalido(self):
12
+ self.assertFalse(validar_cnpj("12.345.678/0001-00"))
13
+ self.assertFalse(validar_cnpj("00.000.000/0000-00"))
14
+ self.assertFalse(validar_cnpj("11.111.111/1111-11"))
15
+
16
+ def test_cnpj_com_caracteres_invalidos(self):
17
+ self.assertFalse(validar_cnpj("12.345.678/0001-9a"))
18
+ self.assertFalse(validar_cnpj("12.345.678/0001-@#"))
19
+ self.assertFalse(validar_cnpj("12.345.678/0001-"))
20
+
21
+ def test_cnpj_com_tamanho_incorreto(self):
22
+ self.assertFalse(validar_cnpj("12.345.678/0001"))
23
+ self.assertFalse(validar_cnpj("12.345.678/0001-1099"))
24
+
25
+
26
+ if __name__ == "__main__":
27
+ unittest.main()
@@ -0,0 +1,35 @@
1
+ import unittest
2
+ from cpf_alfacnpj import validar_cnpj_2026 as validar_cnpj
3
+
4
+
5
+ class TestValidarCNPJ(unittest.TestCase):
6
+ def test_cnpj_valido(self):
7
+ self.assertTrue(validar_cnpj("04.252.011/0001-10"))
8
+ self.assertTrue(validar_cnpj("12.345.678/0001-95"))
9
+
10
+ def test_cnpj_invalido(self):
11
+ self.assertFalse(validar_cnpj("12.345.678/0001-00"))
12
+ self.assertFalse(validar_cnpj("00.000.000/0000-00"))
13
+ self.assertFalse(validar_cnpj("11.111.111/1111-11"))
14
+
15
+ def test_cnpj_com_letras_valido(self):
16
+ self.assertTrue(validar_cnpj("AB.C4A.678/0001-60")) # Exemplo com letras válidas
17
+ self.assertTrue(validar_cnpj("12.34A.678/0001-00")) # Exemplo com letras válidas
18
+ self.assertTrue(validar_cnpj("12.34A.678/0001-00")) # Exemplo com letras válidas
19
+
20
+ def test_cnpj_com_letras_invalido(self):
21
+ self.assertFalse(validar_cnpj("12.34A.678/0001-9Z")) # Exemplo com letras inválidas
22
+ self.assertFalse(validar_cnpj("12.ABC.678/0001-00")) # Exemplo com letras inválidas
23
+
24
+ def test_cnpj_com_caracteres_invalidos(self):
25
+ self.assertFalse(validar_cnpj("12.345.678/0001-9a"))
26
+ self.assertFalse(validar_cnpj("12.345.678/0001-@#"))
27
+ self.assertFalse(validar_cnpj("12.345.678/0001-"))
28
+
29
+ def test_cnpj_com_tamanho_incorreto(self):
30
+ self.assertFalse(validar_cnpj("12.345.678/0001"))
31
+ self.assertFalse(validar_cnpj("12.345.678/0001-1099"))
32
+
33
+
34
+ if __name__ == "__main__":
35
+ unittest.main()
@@ -0,0 +1,25 @@
1
+ import unittest
2
+ from cpf_alfacnpj import validar_cpf
3
+
4
+ class TestValidarCPF(unittest.TestCase):
5
+
6
+ def test_cpf_valido(self):
7
+ self.assertTrue(validar_cpf("111.444.777-35"))
8
+ self.assertTrue(validar_cpf("123.456.789-09"))
9
+
10
+ def test_cpf_invalido(self):
11
+ self.assertFalse(validar_cpf("123.456.789-00"))
12
+ self.assertFalse(validar_cpf("000.000.000-00"))
13
+ self.assertFalse(validar_cpf("111.111.111-11"))
14
+
15
+ def test_cpf_com_caracteres_invalidos(self):
16
+ self.assertFalse(validar_cpf("123.456.789-0a"))
17
+ self.assertFalse(validar_cpf("123.456.789-@#"))
18
+ self.assertFalse(validar_cpf("123.456.789-"))
19
+
20
+ def test_cpf_com_tamanho_incorreto(self):
21
+ self.assertFalse(validar_cpf("123.456.789"))
22
+ self.assertFalse(validar_cpf("123.456.789-0912"))
23
+
24
+ if __name__ == "__main__":
25
+ unittest.main()