-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.py
More file actions
384 lines (308 loc) · 10.9 KB
/
generator.py
File metadata and controls
384 lines (308 loc) · 10.9 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
""" Copyright (c) 2023 Geovanni Santamaria. All Rights Reserved.
Generate CURP following the Normative Instruction of 2006 provided by the Ministry
of the Interior of the United Mexican States.
"""
from datetime import date, datetime
from unidecode import unidecode
from utils import entities, prepositions, profanities
class YearError(Exception):
"""
This class throws an exception.
Args:
Exception (str): Show a message.
"""
def __init__(self, message="The year cannot be greater than the current date."):
super().__init__(message)
class LengthError(Exception):
"""
This class throws an exception.
Args:
Exception (str): Show a message.
"""
def __init__(self, message="The length does not comply with the established."):
super().__init__(message)
def special_character(func):
"""
This decorator replaces the value of the found function that matches the set.
Args:
func (function): Takes a function and its arguments.
"""
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
_special_character = set("ñ/-.")
_special_character = "".join(
"x" if letter in _special_character else letter for letter in result
)
return _special_character
return wrapper
class Generator:
"""
This class generates a Mexican CURP.
Attributes:
name (str): Person's first name.
lastname (str): Person's paternal surname.
mlastname (str): Person's maternal surname.
day (int): Person's day of birth.
month (int): Person's month of birth.
year (int): Person's year of birth.
gender (str): Person's gender.
state (str): State where the person was born.
"""
def __init__(
self,
name: str,
lastname: str,
mlastname: str,
day: int,
month: int,
year: int,
gender: str,
state: str,
) -> None:
self.name = name
self.lastname = lastname
self.mlastname = mlastname
self.day = day
self.month = month
self.gender = gender
self.year = year
self.state = state
def _clean_data(self, value) -> str:
"""
Filters prepositions, conjunctions found in the first names and surnames.
Args:
value (str): Splits a string into a list.
Returns:
str: A string that has been filtered.
"""
cleaned = filter(lambda value: value not in prepositions.prep, value.split())
return " ".join(list(cleaned))
def _data(self) -> dict:
"""
Create a dictionary with lowercase values and call a function to filter.
Returns:
dict: A filtered dictionary.
"""
data = {
"name": self.name,
"lastname": self.lastname,
"mlastname": self.mlastname,
"gender": self.gender,
}
data = {key: self._clean_data(value.lower()) for key, value in data.items()}
data |= {"state": self.state.lower()}
return data
def _filter_len(self, value) -> str:
"""
Take the first value from the list if the value is found in the set
and the length is greater than 1.
Args:
value (str): Split a string into a list.
Returns:
str: First string in a list depending on its length.
"""
if len(result := value.split()) > 1:
result = [
result[1] if unidecode(result[0]) in ("maria", "jose") else result[0]
]
return "".join(result)
def _first_letter(self, letter) -> str:
"""
Take the first character of the string; if none is found, return "x".
Args:
letter (str): A string.
Returns:
str: First character of a string.
"""
return letter[0] if letter else "x"
def _filter_vowels(self, value, is_vowel=False) -> str:
"""
If the letter is a vowel, filter the vowels; otherwise, filter everything
that is not a vowel.
Args:
value (str): Removes the first character from the string.
is_vowel (bool, optional): Check if it is vowel. Defaults to False.
Returns:
str: First letter it finds.
"""
value = value[1:]
vowels = set("aáeéiíoóuúü-/.")
if is_vowel:
# vowels = vowels.union("-", "/", ".")
search = filter(lambda letter: letter in vowels, value)
else:
search = filter(lambda letter: letter not in vowels, value)
return next(search, "x")
@special_character
def _lastname(self, consonant=False) -> str:
"""
If it's not a consonant, it returns the first letter and filters the first vowel
found; otherwise, it filters the first letter that is not found in the vowels.
Args:
consonant (bool, optional): Check if it is a consonant. Defaults to False.
Raises:
LengthError: If the length does not meet the established criteria.
Returns:
str: One or another string depending on a condition.
"""
lastname = self._data()["lastname"]
lastname = self._filter_len(lastname)
if not lastname:
raise LengthError()
return (
self._first_letter(lastname) + self._filter_vowels(lastname, True)
if not consonant
else self._filter_vowels(lastname)
)
@special_character
def _mlastname(self, consonant=False) -> str:
"""
If it's not a consonant, it filters the first vowel it finds; otherwise,
it returns the first letter.
Args:
consonant (bool, optional): Check if it is a consonant. Defaults to False.
Returns:
str: One or another string depending on a condition.
"""
mlastname = self._data()["mlastname"]
return (
self._first_letter(mlastname)
if not consonant
else self._filter_vowels(mlastname)
)
@special_character
def _name(self, consonant=False) -> str:
"""
If it's not a consonant, it filters the first vowel it finds; otherwise,
it returns the first letter.
Args:
consonant (bool, optional): _description_. Defaults to False.
Raises:
LengthError: If the length does not meet the established criteria.
Returns:
str: One or another string depending on a condition.
"""
name = self._data()["name"]
name = self._filter_len(name)
if not name:
raise LengthError()
return self._first_letter(name) if not consonant else self._filter_vowels(name)
def _group_a(self) -> str:
"""
Filters and replaces the dissonant words that are generated when you
concatenate the surnames and the person's name.
Raises:
LengthError: If the length does not meet the established criteria.
Returns:
str: First the value that was found.
"""
group_values = unidecode(self._lastname() + self._mlastname() + self._name())
if len(group_values) != 4:
raise LengthError()
if group_values in profanities.words.keys():
group_values = next(
v for k, v in profanities.words.items() if k == group_values
)
return group_values
def _birthdate(self) -> str:
"""
Check if the year is greater than the current date.
Raises:
YearError: Year greater than the current date.
Returns:
str: Datetime a string.
"""
birth = date(self.year, self.month, self.day)
if (birthdate := birth) > datetime.date(datetime.now()):
raise YearError()
return birthdate.strftime("%y" + "%m" + "%d")
def _gender(self) -> str:
"""
Check if the gender is in one set or another.
Raises:
LengthError: If the length does not meet the established criteria.
Returns:
str: One or another string depending on a condition.
"""
if (gender := self._data()["gender"]) in ("mujer", "m", "femenino", "f"):
gender = "m"
elif gender in ("hombre", "h", "masculino"):
gender = "h"
else:
raise LengthError()
return gender
def _entities(self) -> str:
"""
Filter the Mexican federal entities.
Raises:
LengthError: If the length does not meet the established criteria.
Returns:
str: Value that matches in the dictionary.
"""
if (state := unidecode(self._data()["state"])) in entities.renapo.keys():
state = next(v for k, v in entities.renapo.items() if k == state)
else:
raise LengthError()
return state
def _group_b(self) -> str:
"""
A string with the first consonants found, excluding the first letter.
Raises:
LengthError: If the length does not meet the established criteria.
Returns:
str: A string of the first consonants.
"""
fln = self._lastname(True) + self._mlastname(True) + self._name(True)
if len(fln) != 3:
raise LengthError()
return fln
def _century(self) -> str:
"""
Check the 21st century.
Returns:
str: Zero if not within range
"""
return "a" if self.year in range(2001, 2100) else "0"
def _group_all(self) -> str:
"""
Concatenate the values of the functions.
Returns:
str: A string with the values of the functions.
"""
return (
self._group_a()
+ self._birthdate()
+ self._gender()
+ self._entities()
+ self._group_b()
+ self._century()
)
def _verification_digit(self) -> str:
"""
Calculates the verification digit.
Returns:
str: Concatenate CURP and the verification digit.
"""
abc = "0123456789abcdefghijklmn&opqrstuvwxyz ñ"
curp = self._group_all()
xurp = [abc.find(value) for value in reversed(curp + "0")]
residue = sum((x + 1) * y for x, y in enumerate(xurp)) % 10
result = 10 - residue if residue != 10 else 0
return curp + str(result)
def printer(self):
"""
Print Data
"""
print(
f"""\n
+ + + + + + + + + + + + + + + + + +
+ {self._verification_digit().upper()} +
+ + + + + + + + + + + + + + + + + +
Name: {self.name}
Lastname: {self.lastname}
Lastname: {self.mlastname}
Birthdate: {self._birthdate()}
Gender: {self.gender}
State: {self.state}
+ + + + + + + + + + + + + + + + + +"""
)