forked from leogregianin/validate-docbr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPF.py
77 lines (53 loc) · 2.11 KB
/
CPF.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from typing import List
from .BaseDoc import BaseDoc
from random import sample
class CPF(BaseDoc):
"""Classe referente ao Cadastro de Pessoas Físicas (CPF)."""
def __init__(self, repeated_digits: bool = False):
self.digits = list(range(10))
self.repeated_digits = repeated_digits
def validate(self, doc: str = '') -> bool:
"""Validar CPF."""
if not self._validate_input(doc, ['.', '-']):
return False
doc = list(self._only_digits(doc))
if len(doc) != 11:
return False
if not self.repeated_digits and self._check_repeated_digits(doc):
return False
return self._generate_first_digit(doc) == doc[9] \
and self._generate_second_digit(doc) == doc[10]
def generate(self, mask: bool = False) -> str:
"""Gerar CPF."""
# Os nove primeiros dígitos
cpf = [str(sample(self.digits, 1)[0]) for i in range(9)]
# Gerar os dígitos verificadores
cpf.append(self._generate_first_digit(cpf))
cpf.append(self._generate_second_digit(cpf))
cpf = "".join(cpf)
return self.mask(cpf) if mask else cpf
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:])
def _generate_first_digit(self, doc: list) -> str:
"""Gerar o primeiro dígito verificador do CPF."""
sum = 0
for i in range(10, 1, -1):
sum += int(doc[10 - i]) * i
sum = (sum * 10) % 11
if sum == 10:
sum = 0
return str(sum)
def _generate_second_digit(self, doc: list) -> str:
"""Gerar o segundo dígito verificador do CPF."""
sum = 0
for i in range(11, 1, -1):
sum += int(doc[11 - i]) * i
sum = (sum * 10) % 11
if sum == 10:
sum = 0
return str(sum)
def _check_repeated_digits(self, doc: List[str]) -> bool:
"""Verifica se é um CPF com números repetidos.
Exemplo: 111.111.111-11"""
return len(set(doc)) == 1