Skip to content

Commit 2348533

Browse files
committed
Rename name to firstname and surname to lastname.
1 parent 96bc9c5 commit 2348533

File tree

5 files changed

+151
-139
lines changed

5 files changed

+151
-139
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ from codicefiscale import codicefiscale
3636
```
3737
### Encode
3838
```python
39-
codicefiscale.encode(surname="Caccamo", name="Fabio", sex="M", birthdate="03/04/1985", birthplace="Torino")
39+
codicefiscale.encode(
40+
lastname="Caccamo",
41+
firstname="Fabio",
42+
gender="M",
43+
birthdate="03/04/1985",
44+
birthplace="Torino",
45+
)
4046

4147
# "CCCFBA85D03L219P"
4248
```
@@ -46,7 +52,7 @@ codicefiscale.decode("CCCFBA85D03L219P")
4652

4753
# {
4854
# "code": "CCCFBA85D03L219P",
49-
# "sex": "M",
55+
# "gender": "M",
5056
# "birthdate": datetime.datetime(1985, 4, 3, 0, 0),
5157
# "birthplace": {
5258
# "name": "TORINO"
@@ -65,8 +71,8 @@ codicefiscale.decode("CCCFBA85D03L219P")
6571
# ],
6672
# "raw": {
6773
# "code": "CCCFBA85D03L219P",
68-
# "surname": "CCC",
69-
# "name": "FBA",
74+
# "lastname": "CCC",
75+
# "firstname": "FBA",
7076
# "birthdate": "85D03",
7177
# "birthdate_year": "85"
7278
# "birthdate_month": "D",

codicefiscale/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
def _encode_from_args(args: argparse.Namespace) -> None:
1111
try:
1212
cf = codicefiscale.encode(
13-
surname=args.lastname,
14-
name=args.firstname,
15-
sex=args.gender,
13+
lastname=args.lastname,
14+
firstname=args.firstname,
15+
gender=args.gender,
1616
birthdate=args.birthdate,
1717
birthplace=args.birthplace,
1818
)

codicefiscale/codicefiscale.py

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ def _get_indexed_data() -> (
124124

125125
CODICEFISCALE_RE: Pattern[str] = re.compile(
126126
r"^"
127-
r"(?P<surname>[a-z]{3})"
128-
r"(?P<name>[a-z]{3})"
127+
r"(?P<lastname>[a-z]{3})"
128+
r"(?P<firstname>[a-z]{3})"
129129
r"(?P<birthdate>(?P<birthdate_year>[a-z\d]{2})(?P<birthdate_month>[abcdehlmprst]{1})(?P<birthdate_day>[a-z\d]{2}))" # noqa: B950, E501
130130
r"(?P<birthplace>[a-z]{1}[a-z\d]{3})"
131131
r"(?P<cin>[a-z]{1})$",
@@ -236,55 +236,55 @@ def _get_omocodes(code: str) -> list[str]:
236236
return codes
237237

238238

239-
def encode_surname(surname: str) -> str:
239+
def encode_lastname(lastname: str) -> str:
240240
"""
241-
Encode surname to the code used in italian fiscal code.
241+
Encode lastname to the code used in italian fiscal code.
242242
243-
:param surname: The surname
244-
:type surname: string
243+
:param lastname: The lastname
244+
:type lastname: string
245245
246246
:returns: The code used in italian fiscal code
247247
:rtype: string
248248
"""
249-
surname_slug = slugify(surname)
250-
surname_consonants = _get_consonants(surname_slug)
251-
surname_vowels = _get_vowels(surname_slug)
252-
surname_code = _get_consonants_and_vowels(surname_consonants, surname_vowels)
253-
return surname_code
249+
lastname_slug = slugify(lastname)
250+
lastname_consonants = _get_consonants(lastname_slug)
251+
lastname_vowels = _get_vowels(lastname_slug)
252+
lastname_code = _get_consonants_and_vowels(lastname_consonants, lastname_vowels)
253+
return lastname_code
254254

255255

256-
def encode_name(name: str) -> str:
256+
def encode_firstname(firstname: str) -> str:
257257
"""
258-
Encodes name to the code used in italian fiscal code.
258+
Encodes firstname to the code used in italian fiscal code.
259259
260-
:param name: The name
261-
:type name: string
260+
:param firstname: The firstname
261+
:type firstname: string
262262
263263
:returns: The code used in italian fiscal code
264264
:rtype: string
265265
"""
266-
name_slug = slugify(name)
267-
name_consonants = _get_consonants(name_slug)
266+
firstname_slug = slugify(firstname)
267+
firstname_consonants = _get_consonants(firstname_slug)
268268

269-
if len(name_consonants) > 3:
270-
del name_consonants[1]
269+
if len(firstname_consonants) > 3:
270+
del firstname_consonants[1]
271271

272-
name_vowels = _get_vowels(name_slug)
273-
name_code = _get_consonants_and_vowels(name_consonants, name_vowels)
274-
return name_code
272+
firstname_vowels = _get_vowels(firstname_slug)
273+
firstname_code = _get_consonants_and_vowels(firstname_consonants, firstname_vowels)
274+
return firstname_code
275275

276276

277277
def encode_birthdate(
278278
birthdate: datetime | str | None,
279-
sex: Literal["m", "M", "f", "F"],
279+
gender: Literal["m", "M", "f", "F"],
280280
) -> str:
281281
"""
282282
Encodes birthdate to the code used in italian fiscal code.
283283
284284
:param birthdate: The birthdate
285285
:type birthdate: datetime or string
286-
:param sex: The sex, 'M' or 'F'
287-
:type sex: string
286+
:param gender: The gender, 'M' or 'F'
287+
:type gender: string
288288
289289
:returns: The code used in italian fiscal code
290290
:rtype: string
@@ -295,15 +295,15 @@ def encode_birthdate(
295295
if not date:
296296
raise ValueError("[codicefiscale] 'date' argument cant be None")
297297

298-
if not sex:
299-
raise ValueError("[codicefiscale] 'sex' argument cant be None")
300-
sex_code = sex.upper()
301-
if sex_code not in ("M", "F"):
302-
raise ValueError("[codicefiscale] 'sex' argument must be 'M' or 'F'")
298+
if not gender:
299+
raise ValueError("[codicefiscale] 'gender' argument cant be None")
300+
gender_code = gender.upper()
301+
if gender_code not in ("M", "F"):
302+
raise ValueError("[codicefiscale] 'gender' argument must be 'M' or 'F'")
303303

304304
year_code = str(date.year)[2:]
305305
month_code = _MONTHS[date.month - 1]
306-
day_code = str(date.day + (40 if sex_code == "F" else 0)).zfill(2).upper()
306+
day_code = str(date.day + (40 if gender_code == "F" else 0)).zfill(2).upper()
307307
date_code = f"{year_code}{month_code}{day_code}"
308308
return date_code
309309

@@ -372,21 +372,21 @@ def encode_cin(code: str) -> str:
372372

373373

374374
def encode(
375-
surname: str,
376-
name: str,
377-
sex: Literal["m", "M", "f", "F"],
375+
lastname: str,
376+
firstname: str,
377+
gender: Literal["m", "M", "f", "F"],
378378
birthdate: datetime | str | None,
379379
birthplace: str,
380380
) -> str:
381381
"""
382382
Encodes the italian fiscal code.
383383
384-
:param surname: The surname
385-
:type surname: string
386-
:param name: The name
387-
:type name: string
388-
:param sex: The sex, 'M' or 'F'
389-
:type sex: string
384+
:param lastname: The lastname
385+
:type lastname: string
386+
:param firstname: The firstname
387+
:type firstname: string
388+
:param gender: The gender, 'M' or 'F'
389+
:type gender: string
390390
:param birthdate: The birthdate
391391
:type birthdate: datetime or string
392392
:param birthplace: The birthplace
@@ -396,11 +396,11 @@ def encode(
396396
:rtype: string
397397
"""
398398

399-
surname_code = encode_surname(surname)
400-
name_code = encode_name(name)
401-
birthdate_code = encode_birthdate(birthdate, sex)
399+
lastname_code = encode_lastname(lastname)
400+
firstname_code = encode_firstname(firstname)
401+
birthdate_code = encode_birthdate(birthdate, gender)
402402
birthplace_code = encode_birthplace(birthplace, birthdate)
403-
code = f"{surname_code}{name_code}{birthdate_code}{birthplace_code}"
403+
code = f"{lastname_code}{firstname_code}{birthdate_code}{birthplace_code}"
404404
cin_code = encode_cin(code)
405405
code = f"{code}{cin_code}"
406406

@@ -429,8 +429,8 @@ def decode_raw(code: str) -> dict[str, str]:
429429

430430
data = {
431431
"code": code,
432-
"surname": match["surname"],
433-
"name": match["name"],
432+
"lastname": match["lastname"],
433+
"firstname": match["firstname"],
434434
"birthdate": match["birthdate"],
435435
"birthdate_year": match["birthdate_year"],
436436
"birthdate_month": match["birthdate_month"],
@@ -462,9 +462,9 @@ def decode(code: str) -> dict[str, Any]:
462462

463463
if birthdate_day > 40:
464464
birthdate_day -= 40
465-
sex = "F"
465+
gender = "F"
466466
else:
467-
sex = "M"
467+
gender = "M"
468468

469469
current_year = datetime.now().year
470470
current_year_century_prefix = str(current_year)[0:-2]
@@ -496,7 +496,7 @@ def decode(code: str) -> dict[str, Any]:
496496
data = {
497497
"code": code,
498498
"omocodes": _get_omocodes(code),
499-
"sex": sex,
499+
"gender": gender,
500500
"birthdate": birthdate,
501501
"birthplace": birthplace,
502502
"raw": raw,

tests/test_cli.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def test_decode_without_omocodes(self):
9898
"""
9999
{
100100
"code": "RSSMRA90A01H501W",
101-
"sex": "M",
101+
"gender": "M",
102102
"birthdate": "1990-01-01T00:00:00",
103103
"birthplace": {
104104
"active": false,
@@ -116,8 +116,8 @@ def test_decode_without_omocodes(self):
116116
},
117117
"raw": {
118118
"code": "RSSMRA90A01H501W",
119-
"surname": "RSS",
120-
"name": "MRA",
119+
"lastname": "RSS",
120+
"firstname": "MRA",
121121
"birthdate": "90A01",
122122
"birthdate_year": "90",
123123
"birthdate_month": "A",
@@ -133,7 +133,7 @@ def test_decode_without_omocodes(self):
133133
output_data,
134134
{
135135
"code": "RSSMRA90A01H501W",
136-
"sex": "M",
136+
"gender": "M",
137137
"birthdate": "1990-01-01T00:00:00",
138138
"birthplace": {
139139
"active": False,
@@ -149,8 +149,8 @@ def test_decode_without_omocodes(self):
149149
},
150150
"raw": {
151151
"code": "RSSMRA90A01H501W",
152-
"surname": "RSS",
153-
"name": "MRA",
152+
"lastname": "RSS",
153+
"firstname": "MRA",
154154
"birthdate": "90A01",
155155
"birthdate_year": "90",
156156
"birthdate_month": "A",
@@ -170,7 +170,7 @@ def test_decode_without_omocodes_from_command_line(self):
170170
"""
171171
{
172172
"code": "RSSMRA90A01H501W",
173-
"sex": "M",
173+
"gender": "M",
174174
"birthdate": "1990-01-01T00:00:00",
175175
"birthplace": {
176176
"active": false,
@@ -188,8 +188,8 @@ def test_decode_without_omocodes_from_command_line(self):
188188
},
189189
"raw": {
190190
"code": "RSSMRA90A01H501W",
191-
"surname": "RSS",
192-
"name": "MRA",
191+
"lastname": "RSS",
192+
"firstname": "MRA",
193193
"birthdate": "90A01",
194194
"birthdate_year": "90",
195195
"birthdate_month": "A",
@@ -206,7 +206,7 @@ def test_decode_without_omocodes_from_command_line(self):
206206
output_data,
207207
{
208208
"code": "RSSMRA90A01H501W",
209-
"sex": "M",
209+
"gender": "M",
210210
"birthdate": "1990-01-01T00:00:00",
211211
"birthplace": {
212212
"active": False,
@@ -222,8 +222,8 @@ def test_decode_without_omocodes_from_command_line(self):
222222
},
223223
"raw": {
224224
"code": "RSSMRA90A01H501W",
225-
"surname": "RSS",
226-
"name": "MRA",
225+
"lastname": "RSS",
226+
"firstname": "MRA",
227227
"birthdate": "90A01",
228228
"birthdate_year": "90",
229229
"birthdate_month": "A",
@@ -378,7 +378,7 @@ def test_decode_with_omocodes(self):
378378
"RSSMRAVLALMHRL1F",
379379
"RSSMRAVLALMHRLMX"
380380
],
381-
"sex": "M",
381+
"gender": "M",
382382
"birthdate": "1990-01-01T00:00:00",
383383
"birthplace": {
384384
"active": false,
@@ -396,8 +396,8 @@ def test_decode_with_omocodes(self):
396396
},
397397
"raw": {
398398
"code": "RSSMRA90A01H501W",
399-
"surname": "RSS",
400-
"name": "MRA",
399+
"lastname": "RSS",
400+
"firstname": "MRA",
401401
"birthdate": "90A01",
402402
"birthdate_year": "90",
403403
"birthdate_month": "A",
@@ -543,7 +543,7 @@ def test_decode_with_omocodes(self):
543543
"RSSMRAVLALMHRL1F",
544544
"RSSMRAVLALMHRLMX",
545545
],
546-
"sex": "M",
546+
"gender": "M",
547547
"birthdate": "1990-01-01T00:00:00",
548548
"birthplace": {
549549
"active": False,
@@ -559,8 +559,8 @@ def test_decode_with_omocodes(self):
559559
},
560560
"raw": {
561561
"code": "RSSMRA90A01H501W",
562-
"surname": "RSS",
563-
"name": "MRA",
562+
"lastname": "RSS",
563+
"firstname": "MRA",
564564
"birthdate": "90A01",
565565
"birthdate_year": "90",
566566
"birthdate_month": "A",

0 commit comments

Comments
 (0)