Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5a0c362

Browse files
CPF Generator add and CPF Checker tweaks
1 parent 5c18501 commit 5a0c362

File tree

3 files changed

+94
-16
lines changed

3 files changed

+94
-16
lines changed

‎Problem Solving Scripts/CPF_Checker.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,25 +84,18 @@ class CPF_Validator_N:
8484
This class is used to validate CPF numbers.
8585
"""
8686

87-
def __init__(self, userCPF):
87+
def __init__(self, cpf: str):
8888
"""
8989
This function is used to initialize the class.
9090
"""
91-
self.userCPF = userCPF
92-
93-
@property
94-
def validateCPF(self):
95-
"""
96-
This function is used to return the CPF validation.
97-
"""
98-
return self.userCPF
91+
self.validateCPF(cpf)
9992

10093
validCPF = None
10194
partiallyValidation = None
10295

10396
@staticmethod
10497
# Validates a CPF number with the easy way.
105-
def validateCPF(userCPF):
98+
def validateCPF(userCPF: str):
10699
"""
107100
This function is used to validate and clean the CPF number.
108101
"""
@@ -114,7 +107,7 @@ def validateCPF(userCPF):
114107
if len(userCPF) != 11:
115108
validCPF = False
116109
logThis("CPF is NOT valid.")
117-
return validCPF
110+
return False
118111
else:
119112

120113
# Separate all numbers from userCPF into a list.
@@ -152,19 +145,19 @@ def validateCPF(userCPF):
152145
logThis(f"Merged result: {mergeRes}")
153146

154147
if mergeRes == userCPF and partiallyValidation == True:
155-
logThis("CPF is valid.")
156148
validCPF = True
157-
return validCPF
149+
logThis(f"CPF is valid. {validCPF}")
150+
return True
158151
else:
159-
logThis("CPF is NOT valid.")
160152
validCPF = False
161-
return validCPF
153+
logThis(f"CPF is NOT valid. {validCPF}")
154+
return False
162155

163156

164157
if __name__ == "__main__":
165158
"""
166159
This function is used to run the program.
167160
"""
168161
if debugMode:
169-
sampleCPF = "113.314.390-35"
162+
sampleCPF = "510088499011"
170163
CPF_Validator_N.validateCPF(sampleCPF)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env python3
2+
3+
""" Nicolas Mendes - Jan 26 2022
4+
5+
Based on https://www.alura.com.br/conteudo/python-validacao-dados#:~:text=Criando%20um%20novo%20Python%20file,contr%C3%A1rio%20o%20retorno%20ser%C3%A1%20False%20, https://gist.github.com/lucascnr/24c70409908a31ad253f97f9dd4c6b7c
6+
7+
The CPF calculation is based on the final 2 digits.
8+
To validate, take the first 9 digits of the CPF, generate the 2 digits and save in a new CPF.
9+
Compare if the CPF sent is the same as the CPF generated.
10+
If true, the CPF is valid, otherwise invalid.
11+
12+
SAMPLE CPF: 113.314.390-35
13+
"""
14+
15+
import random
16+
from CPF_Checker import CPF_Validator_N as isThisValid
17+
18+
# WARNING: Disable in production!
19+
debugMode = True
20+
21+
22+
def logThis(message):
23+
"""
24+
This function is used to log messages if debug is enabled.
25+
"""
26+
if debugMode:
27+
print(message)
28+
29+
30+
class CPF_GEN:
31+
"""
32+
This class is used to generate CPF numbers.
33+
"""
34+
35+
logThis("Generating CPF...")
36+
37+
def __init__(self):
38+
"""
39+
This function is used to initialize the class.
40+
"""
41+
self.generateCPF()
42+
43+
@staticmethod
44+
def generateCPF():
45+
"""
46+
This function is used to generate the CPF.
47+
"""
48+
49+
# Start with random sequence between 0 and 9
50+
cpf = [random.randrange(10) for _ in range(9)]
51+
52+
for _ in range(2):
53+
for i, v in enumerate(cpf):
54+
res = (len(cpf) + 1 - i) * v
55+
56+
if res > 9:
57+
res = 0
58+
else:
59+
res = 11 - res
60+
61+
cpf.append(res)
62+
63+
return "".join(str(x) for x in cpf)
64+
65+
def checkCPF():
66+
if debugMode:
67+
forceCPF = "113.314.390-35"
68+
69+
if isThisValid.validateCPF(str(CPF_GEN.generateCPF())) != True:
70+
CPF_GEN.generateCPF()
71+
logThis("CPF is invalid, generating another one...")
72+
# logThis(f"Test Import: {isThisValid.validateCPF(str(CPF_GEN.generateCPF()))}")
73+
return isThisValid.validateCPF(str(CPF_GEN.generateCPF()))
74+
else:
75+
logThis("CPF is valid!")
76+
return CPF_GEN.generateCPF()
77+
78+
79+
if __name__ == "__main__":
80+
"""
81+
This function is used to run the program.
82+
"""
83+
CPF_GEN.generateCPF()
84+
# CPF_GEN.checkCPF()
85+
logThis(CPF_GEN.checkCPF())
3.91 KB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /