-
Notifications
You must be signed in to change notification settings - Fork 51
/
RENAVAM.py
51 lines (35 loc) · 1.32 KB
/
RENAVAM.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
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)."""
def __init__(self):
self.digits = list(range(10))
def validate(self, doc: str = '') -> bool:
"""Validar RENAVAM."""
if not self._validate_input(doc, [' ']):
return False
doc = self._only_digits(doc)
if len(doc) != 11:
return False
last_digit = self._generate_last_digit(doc)
return last_digit == doc[10]
def generate(self, mask: bool = False) -> str:
"""Gerar Renavam."""
renavam = [str(sample(self.digits, 1)[0]) for i in range(10)]
renavam.append(self._generate_last_digit(renavam))
renavam = ''.join(renavam)
return renavam
def mask(self, doc: str = '') -> str:
"""Coloca a máscara de Renavam na variável doc."""
return f"{doc[:10]}-{doc[10]}"
def _generate_last_digit(self, doc: Union[str, list]) -> str:
"""Gerar o dígito verificador do Renavam."""
sequence = '3298765432'
sum = 0
for i in range(0, 10):
sum += int(doc[i]) * int(sequence[i])
rest = (sum * 10) % 11
if rest == 10:
rest = 0
return str(rest)