Skip to content

Commit

Permalink
style: Aplica linter
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarofpp committed Feb 24, 2024
1 parent bfef512 commit afdcd86
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 82 deletions.
30 changes: 2 additions & 28 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,2 @@
bleach==4.1.0
certifi==2023.7.22
cffi==1.15.0
charset-normalizer==2.0.7
colorama==0.4.4
coverage==6.0.2
cryptography==39.0.1
docutils==0.18
idna==3.3
importlib-metadata==4.8.1
jeepney==0.7.1
keyring==23.2.1
packaging==21.0
pkginfo==1.7.1
pycparser==2.20
Pygments==2.10.0
pyparsing==3.0.2
readme-renderer==30.0
requests==2.26.0
requests-toolbelt==0.9.1
rfc3986==1.5.0
SecretStorage==3.3.1
six==1.16.0
tqdm==4.62.3
twine==3.4.2
urllib3==1.26.7
webencodings==0.5.1
zipp==3.6.0
pytest==7.4.4
pytest-cov==4.1.0
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import setuptools


with open("README.md", "r") as fh:
with open("README.md") as fh:
long_description = fh.read()

setuptools.setup(
Expand Down
25 changes: 19 additions & 6 deletions validate_docbr/BaseDoc.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from abc import ABC
from abc import ABC, abstractmethod
from typing import List


class BaseDoc(ABC):
"""Classe base para todas as classes referentes a documentos."""

@abstractmethod
def validate(self, doc: str = '') -> bool:
"""Método para validar o documento desejado."""
pass
Expand All @@ -13,43 +14,55 @@ def validate_list(self, docs: List[str]) -> List[bool]:
"""Método para validar uma lista de documentos desejado."""
return [self.validate(doc) for doc in docs]

@abstractmethod
def generate(self, mask: bool = False) -> str:
"""Método para gerar um documento válido."""
pass

def generate_list(self, n: int = 1, mask: bool = False, repeat: bool = False) -> list:
def generate_list(
self,
n: int = 1,
mask: bool = False,
repeat: bool = False
) -> list:
"""Gerar uma lista do mesmo documento."""
doc_list = []

if n <= 0:
return doc_list

for i in range(n):
for _ in range(n):
doc_list.append(self.generate(mask))

while not repeat:
doc_set = set(doc_list)
unique_values = len(doc_set)

if unique_values < n:
doc_list = list(doc_set) + self.generate_list((n - unique_values), mask, repeat)
doc_list = list(doc_set) + self.generate_list(
(n - unique_values),
mask,
repeat
)
else:
repeat = True

return doc_list

@abstractmethod
def mask(self, doc: str = '') -> str:
"""Mascara o documento enviado"""
pass

def _only_digits(self, doc: str = '') -> str:
"""Remove os outros caracteres que não sejam dígitos."""
return "".join([x for x in doc if x.isdigit()])
return ''.join([x for x in doc if x.isdigit()])

def _validate_input(self, input: str, valid_characters: List = None) -> bool:
"""Validar input.
Caso ele possua apenas dígitos e caracteres válidos, retorna True.
Caso possua algum caractere que não seja dígito ou caractere válido, retorna False."""
Caso possua algum caractere que não seja dígito ou caractere válido,
retorna False."""
if valid_characters is None:
valid_characters = ['.', '-', '/', ' ']

Expand Down
5 changes: 3 additions & 2 deletions validate_docbr/CNH.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from .BaseDoc import BaseDoc
from random import sample
from typing import Union

from .BaseDoc import BaseDoc


class CNH(BaseDoc):
"""Classe referente ao Carteira Nacional de Habilitação (CNH)."""
Expand Down Expand Up @@ -35,7 +36,7 @@ def generate(self, mask: bool = False) -> str:

def mask(self, doc: str = '') -> str:
"""Coloca a máscara de CNH na variável doc."""
return "{} {} {} {}".format(doc[:3], doc[3:6], doc[6:9], doc[9:])
return f"{doc[:3]} {doc[3:6]} {doc[6:9]} {doc[9:]}"

def _generate_first_digit(self, doc: Union[str, list]) -> str:
"""Gerar o primeiro dígito verificador da CNH."""
Expand Down
23 changes: 6 additions & 17 deletions validate_docbr/CNPJ.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from .BaseDoc import BaseDoc
from random import sample
from typing import Union

from .BaseDoc import BaseDoc


class CNPJ(BaseDoc):
"""Classe referente ao Cadastro Nacional da Pessoa Jurídica (CNPJ)."""
Expand All @@ -21,11 +22,7 @@ def validate(self, doc: str = '') -> bool:
if len(doc) != 14:
return False

for i in range(10):
if doc.count("{}".format(i)) == 14:
return False

return self._generate_first_digit(doc) == doc[12]\
return self._generate_first_digit(doc) == doc[12] \
and self._generate_second_digit(doc) == doc[13]

def generate(self, mask: bool = False) -> str:
Expand All @@ -43,7 +40,7 @@ def generate(self, mask: bool = False) -> str:

def mask(self, doc: str = '') -> str:
"""Coloca a máscara de CNPJ na variável doc."""
return "{}.{}.{}/{}-{}".format(doc[:2], doc[2:5], doc[5:8], doc[8:12], doc[-2:])
return f"{doc[:2]}.{doc[2:5]}.{doc[5:8]}/{doc[8:12]}-{doc[-2:]}"

def _generate_first_digit(self, doc: Union[str, list]) -> str:
"""Gerar o primeiro dígito verificador do CNPJ."""
Expand All @@ -53,11 +50,7 @@ def _generate_first_digit(self, doc: Union[str, list]) -> str:
sum += int(doc[i]) * self.weights_first[i]

sum = sum % 11

if sum < 2:
sum = 0
else:
sum = 11 - sum
sum = 0 if sum < 2 else 11 - sum

return str(sum)

Expand All @@ -69,10 +62,6 @@ def _generate_second_digit(self, doc: Union[str, list]) -> str:
sum += int(doc[i]) * self.weights_second[i]

sum = sum % 11

if sum < 2:
sum = 0
else:
sum = 11 - sum
sum = 0 if sum < 2 else 11 - sum

return str(sum)
12 changes: 7 additions & 5 deletions validate_docbr/CNS.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .BaseDoc import BaseDoc
from random import sample

from .BaseDoc import BaseDoc


class CNS(BaseDoc):
"""Classe referente ao Cartão Nacional de Saúde (CNS)."""
Expand Down Expand Up @@ -44,13 +45,13 @@ def generate(self, mask: bool = False) -> str:
else:
cns = self._generate_second_case(cns)

cns = "".join(cns)
cns = ''.join(cns)

return self.mask(cns) if mask else cns

def mask(self, doc: str = '') -> str:
"""Coloca a máscara de CPF na variável doc."""
return "{} {} {} {}".format(doc[:3], doc[3:7], doc[7:11], doc[-4:])
return f"{doc[:3]} {doc[3:7]} {doc[7:11]} {doc[-4:]}"

def _generate_first_case(self, cns: list, generate_random=False) -> list:
"""Gera um CNS válido para os casos que se inicia com 1 ou 2."""
Expand Down Expand Up @@ -95,7 +96,8 @@ def _generate_second_case(self, cns: list) -> list:
return self._change_cns(cns, 15 - diff, diff)

def _change_cns(self, cns: list, i: int, val: int) -> list:
"""Altera o CNS recursivamente para que atenda as especificações de validade dele."""
"""Altera o CNS recursivamente para que atenda as especificações de
validade dele."""
if val == 0:
if self._check_cns_valid(cns):
return cns
Expand All @@ -108,7 +110,7 @@ def _change_cns(self, cns: list, i: int, val: int) -> list:
i += 1
return self._change_cns(cns, i, val)

if not cns[i] == '9':
if cns[i] != '9':
cns[i] = str(int(cns[i]) + 1)
val -= (15 - i)
else:
Expand Down
5 changes: 3 additions & 2 deletions validate_docbr/CPF.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from random import sample
from typing import List

from .BaseDoc import BaseDoc
from random import sample


class CPF(BaseDoc):
Expand Down Expand Up @@ -41,7 +42,7 @@ def generate(self, mask: bool = False) -> str:

def mask(self, doc: str = '') -> str:
"""Coloca a máscara de CPF na variável doc."""
return "{}.{}.{}-{}".format(doc[:3], doc[3:6], doc[6:9], doc[-2:])
return f"{doc[:3]}.{doc[3:6]}.{doc[6:9]}-{doc[-2:]}"

def _generate_first_digit(self, doc: list) -> str:
"""Gerar o primeiro dígito verificador do CPF."""
Expand Down
3 changes: 2 additions & 1 deletion validate_docbr/Certidao.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .BaseDoc import BaseDoc
from random import sample

from .BaseDoc import BaseDoc


class Certidao(BaseDoc):
"""Classe referente a Certidão de Nascimento/Casamento/Óbito."""
Expand Down
5 changes: 3 additions & 2 deletions validate_docbr/PIS.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from .BaseDoc import BaseDoc
from random import sample
from typing import Union

from .BaseDoc import BaseDoc


class PIS(BaseDoc):
"""Classe referente ao PIS/NIS/PASEP/NIT."""
Expand Down Expand Up @@ -33,7 +34,7 @@ def generate(self, mask: bool = False) -> str:

def mask(self, doc: str = '') -> str:
"""Coloca a máscara de PIS/NIS/PASEP/NIT na variável doc."""
return "{}.{}.{}-{}".format(doc[:3], doc[3:8], doc[8:10], doc[10:])
return f"{doc[:3]}.{doc[3:8]}.{doc[8:10]}-{doc[10:]}"

def _generate_digit(self, doc: Union[str, list]) -> str:
"""Gerar o dígito verificador do PIS/NIS/PASEP/NIT."""
Expand Down
5 changes: 3 additions & 2 deletions validate_docbr/RENAVAM.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from .BaseDoc import BaseDoc
from random import sample
from typing import Union

from .BaseDoc import BaseDoc


class RENAVAM(BaseDoc):
"""Classe referente ao Registro Nacional de Veículos Automotores (RENAVAM)."""
Expand Down Expand Up @@ -33,7 +34,7 @@ def generate(self, mask: bool = False) -> str:

def mask(self, doc: str = '') -> str:
"""Coloca a máscara de Renavam na variável doc."""
return "{}-{}".format(doc[:10], doc[10])
return f"{doc[:10]}-{doc[10]}"

def _generate_last_digit(self, doc: Union[str, list]) -> str:
"""Gerar o dígito verificador do Renavam."""
Expand Down
38 changes: 28 additions & 10 deletions validate_docbr/TituloEleitoral.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from .BaseDoc import BaseDoc
from random import sample
from typing import List

from .BaseDoc import BaseDoc


class TituloEleitoral(BaseDoc):
"""Classe referente ao Título eleitoral"""
Expand All @@ -24,9 +25,13 @@ def validate(self, doc: str = '') -> bool:
return False

first_check_digit = self._compute_first_check_digit(doc_digits=doc_digits)
second_check_digit = self._compute_second_check_digit(doc_digits=doc_digits, first_check_digit=first_check_digit)
second_check_digit = self._compute_second_check_digit(
doc_digits=doc_digits,
first_check_digit=first_check_digit
)

return first_check_digit == doc_digits[-2] and second_check_digit == doc_digits[-1]
return first_check_digit == doc_digits[-2] \
and second_check_digit == doc_digits[-1]

def generate(self, mask: bool = False) -> str:
"""Método para gerar um título eleitoral válido."""
Expand All @@ -36,8 +41,10 @@ def generate(self, mask: bool = False) -> str:
document_digits.extend(map(int, state_identifier))

first_check_digit = self._compute_first_check_digit(doc_digits=document_digits)
second_check_digit = self._compute_second_check_digit(doc_digits=document_digits,
first_check_digit=first_check_digit)
second_check_digit = self._compute_second_check_digit(
doc_digits=document_digits,
first_check_digit=first_check_digit
)
document_digits.extend([first_check_digit, second_check_digit])

document = ''.join(map(str, document_digits))
Expand All @@ -49,14 +56,17 @@ def generate(self, mask: bool = False) -> str:

def mask(self, doc: str = '') -> str:
"""Mascara o documento enviado"""
return '{} {} {}'.format(doc[0:4], doc[4:8], doc[8:])
return f'{doc[0:4]} {doc[4:8]} {doc[8:]}'

def _compute_first_check_digit(self, doc_digits: List[int]) -> int:
"""Método que calcula o primeiro dígito verificador."""
doc_digits_to_consider = doc_digits[self.first_check_digit_doc_slice]
terms = [
doc_digit * multiplier
for doc_digit, multiplier in zip(doc_digits_to_consider, self.first_check_digit_weights)
for doc_digit, multiplier in zip(
doc_digits_to_consider,
self.first_check_digit_weights
)
]

total = sum(terms)
Expand All @@ -66,12 +76,20 @@ def _compute_first_check_digit(self, doc_digits: List[int]) -> int:

return total % 11

def _compute_second_check_digit(self, doc_digits: List[int], first_check_digit: int) -> int:
def _compute_second_check_digit(
self,
doc_digits: List[int],
first_check_digit: int
) -> int:
"""Método que calcula o segundo dígito verificador."""
doc_digits_to_consider = doc_digits[self.second_check_digit_doc_slice] + [first_check_digit]
doc_digits_to_consider = doc_digits[self.second_check_digit_doc_slice] \
+ [first_check_digit]
terms = [
doc_digit * multiplier
for doc_digit, multiplier in zip(doc_digits_to_consider, self.second_check_digit_weights)
for doc_digit, multiplier in zip(
doc_digits_to_consider,
self.second_check_digit_weights
)
]

total = sum(terms)
Expand Down
Loading

0 comments on commit afdcd86

Please sign in to comment.