-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpycpf.py
42 lines (30 loc) · 944 Bytes
/
pycpf.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
import re
import sys
def isValid(cpf):
if "*" in cpf:
return False
cpf = re.sub(r"[^0-9]", "", cpf)
cpf = cpf.zfill(11)
pList = [x for x in range(10, 1, -1)]
sList = [x for x in range(11, 1, -1)]
cCpf = cpf[:9]
pdigi = sum([int(x) * int(y) for x, y in zip(cCpf, pList)])
pdigi = 0 if pdigi % 11 < 2 else 11 - pdigi % 11
cCpf = cCpf + str(pdigi)
sdigi = sum([int(x) * int(y) for x, y in zip(cCpf, sList)])
sdigi = 0 if sdigi % 11 < 2 else 11 - sdigi % 11
cCpf = cCpf + str(sdigi)
cCpf = cCpf.zfill(11)
return True if cpf == cCpf else False
def cpFormat(cpf):
cpf = re.sub(r"[^0-9\*]", "", cpf)
return str("{}.{}.{}-{}".format(cpf[:3], cpf[3:6], cpf[6:9], cpf[9:]))
def cpFix(x):
y = []
for cpf in x:
cpf = re.sub(r"[^0-9]", "", cpf)
if cpf == "":
cpf = "*" * 11
cpf = cpf.zfill(11)
y.append(cpf)
return y