diff options
Diffstat (limited to 'venv/lib/python3.11/site-packages/faker/providers/ssn/pt_BR')
2 files changed, 64 insertions, 0 deletions
diff --git a/venv/lib/python3.11/site-packages/faker/providers/ssn/pt_BR/__init__.py b/venv/lib/python3.11/site-packages/faker/providers/ssn/pt_BR/__init__.py new file mode 100644 index 0000000..1efac15 --- /dev/null +++ b/venv/lib/python3.11/site-packages/faker/providers/ssn/pt_BR/__init__.py @@ -0,0 +1,64 @@ +from typing import List + +from .. import Provider as SsnProvider + + +def checksum(digits: List[int]) -> int: + """ + Returns the checksum of CPF digits. + References to the algorithm: + https://pt.wikipedia.org/wiki/Cadastro_de_pessoas_f%C3%ADsicas#Algoritmo + https://metacpan.org/source/MAMAWE/Algorithm-CheckDigits-v1.3.0/lib/Algorithm/CheckDigits/M11_004.pm + """ + s = 0 + p = len(digits) + 1 + for i in range(0, len(digits)): + s += digits[i] * p + p -= 1 + + reminder = s % 11 + if reminder == 0 or reminder == 1: + return 0 + else: + return 11 - reminder + + +class Provider(SsnProvider): + """ + Provider for Brazilian SSN also known in Brazil as CPF. + There are two methods Provider.ssn and Provider.cpf + The snn returns a valid number with numbers only + The cpf return a valid number formatted with brazilian mask. eg nnn.nnn.nnn-nn + """ + + def ssn(self) -> str: + digits = self.generator.random.sample(range(10), 9) + + dv = checksum(digits) + digits.append(dv) + digits.append(checksum(digits)) + + return "".join(map(str, digits)) + + def cpf(self) -> str: + c = self.ssn() + return c[:3] + "." + c[3:6] + "." + c[6:9] + "-" + c[9:] + + def rg(self) -> str: + """ + Brazilian RG, return plain numbers. + Check: https://www.ngmatematica.com/2014/02/como-determinar-o-digito-verificador-do.html + """ + + digits = self.generator.random.sample(range(0, 9), 8) + checksum = sum(i * digits[i - 2] for i in range(2, 10)) + last_digit = 11 - (checksum % 11) + + if last_digit == 10: + digits.append("X") + elif last_digit == 11: + digits.append(0) + else: + digits.append(last_digit) + + return "".join(map(str, digits)) diff --git a/venv/lib/python3.11/site-packages/faker/providers/ssn/pt_BR/__pycache__/__init__.cpython-311.pyc b/venv/lib/python3.11/site-packages/faker/providers/ssn/pt_BR/__pycache__/__init__.cpython-311.pyc Binary files differnew file mode 100644 index 0000000..072caa2 --- /dev/null +++ b/venv/lib/python3.11/site-packages/faker/providers/ssn/pt_BR/__pycache__/__init__.cpython-311.pyc |