Skip to content

Commit

Permalink
Rename name to firstname and surname to lastname.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocaccamo committed Mar 27, 2023
1 parent 96bc9c5 commit 2348533
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 139 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ from codicefiscale import codicefiscale
```
### Encode
```python
codicefiscale.encode(surname="Caccamo", name="Fabio", sex="M", birthdate="03/04/1985", birthplace="Torino")
codicefiscale.encode(
lastname="Caccamo",
firstname="Fabio",
gender="M",
birthdate="03/04/1985",
birthplace="Torino",
)

# "CCCFBA85D03L219P"
```
Expand All @@ -46,7 +52,7 @@ codicefiscale.decode("CCCFBA85D03L219P")

# {
# "code": "CCCFBA85D03L219P",
# "sex": "M",
# "gender": "M",
# "birthdate": datetime.datetime(1985, 4, 3, 0, 0),
# "birthplace": {
# "name": "TORINO"
Expand All @@ -65,8 +71,8 @@ codicefiscale.decode("CCCFBA85D03L219P")
# ],
# "raw": {
# "code": "CCCFBA85D03L219P",
# "surname": "CCC",
# "name": "FBA",
# "lastname": "CCC",
# "firstname": "FBA",
# "birthdate": "85D03",
# "birthdate_year": "85"
# "birthdate_month": "D",
Expand Down
6 changes: 3 additions & 3 deletions codicefiscale/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
def _encode_from_args(args: argparse.Namespace) -> None:
try:
cf = codicefiscale.encode(
surname=args.lastname,
name=args.firstname,
sex=args.gender,
lastname=args.lastname,
firstname=args.firstname,
gender=args.gender,
birthdate=args.birthdate,
birthplace=args.birthplace,
)
Expand Down
98 changes: 49 additions & 49 deletions codicefiscale/codicefiscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ def _get_indexed_data() -> (

CODICEFISCALE_RE: Pattern[str] = re.compile(
r"^"
r"(?P<surname>[a-z]{3})"
r"(?P<name>[a-z]{3})"
r"(?P<lastname>[a-z]{3})"
r"(?P<firstname>[a-z]{3})"
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
r"(?P<birthplace>[a-z]{1}[a-z\d]{3})"
r"(?P<cin>[a-z]{1})$",
Expand Down Expand Up @@ -236,55 +236,55 @@ def _get_omocodes(code: str) -> list[str]:
return codes


def encode_surname(surname: str) -> str:
def encode_lastname(lastname: str) -> str:
"""
Encode surname to the code used in italian fiscal code.
Encode lastname to the code used in italian fiscal code.
:param surname: The surname
:type surname: string
:param lastname: The lastname
:type lastname: string
:returns: The code used in italian fiscal code
:rtype: string
"""
surname_slug = slugify(surname)
surname_consonants = _get_consonants(surname_slug)
surname_vowels = _get_vowels(surname_slug)
surname_code = _get_consonants_and_vowels(surname_consonants, surname_vowels)
return surname_code
lastname_slug = slugify(lastname)
lastname_consonants = _get_consonants(lastname_slug)
lastname_vowels = _get_vowels(lastname_slug)
lastname_code = _get_consonants_and_vowels(lastname_consonants, lastname_vowels)
return lastname_code


def encode_name(name: str) -> str:
def encode_firstname(firstname: str) -> str:
"""
Encodes name to the code used in italian fiscal code.
Encodes firstname to the code used in italian fiscal code.
:param name: The name
:type name: string
:param firstname: The firstname
:type firstname: string
:returns: The code used in italian fiscal code
:rtype: string
"""
name_slug = slugify(name)
name_consonants = _get_consonants(name_slug)
firstname_slug = slugify(firstname)
firstname_consonants = _get_consonants(firstname_slug)

if len(name_consonants) > 3:
del name_consonants[1]
if len(firstname_consonants) > 3:
del firstname_consonants[1]

name_vowels = _get_vowels(name_slug)
name_code = _get_consonants_and_vowels(name_consonants, name_vowels)
return name_code
firstname_vowels = _get_vowels(firstname_slug)
firstname_code = _get_consonants_and_vowels(firstname_consonants, firstname_vowels)
return firstname_code


def encode_birthdate(
birthdate: datetime | str | None,
sex: Literal["m", "M", "f", "F"],
gender: Literal["m", "M", "f", "F"],
) -> str:
"""
Encodes birthdate to the code used in italian fiscal code.
:param birthdate: The birthdate
:type birthdate: datetime or string
:param sex: The sex, 'M' or 'F'
:type sex: string
:param gender: The gender, 'M' or 'F'
:type gender: string
:returns: The code used in italian fiscal code
:rtype: string
Expand All @@ -295,15 +295,15 @@ def encode_birthdate(
if not date:
raise ValueError("[codicefiscale] 'date' argument cant be None")

if not sex:
raise ValueError("[codicefiscale] 'sex' argument cant be None")
sex_code = sex.upper()
if sex_code not in ("M", "F"):
raise ValueError("[codicefiscale] 'sex' argument must be 'M' or 'F'")
if not gender:
raise ValueError("[codicefiscale] 'gender' argument cant be None")
gender_code = gender.upper()
if gender_code not in ("M", "F"):
raise ValueError("[codicefiscale] 'gender' argument must be 'M' or 'F'")

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

Expand Down Expand Up @@ -372,21 +372,21 @@ def encode_cin(code: str) -> str:


def encode(
surname: str,
name: str,
sex: Literal["m", "M", "f", "F"],
lastname: str,
firstname: str,
gender: Literal["m", "M", "f", "F"],
birthdate: datetime | str | None,
birthplace: str,
) -> str:
"""
Encodes the italian fiscal code.
:param surname: The surname
:type surname: string
:param name: The name
:type name: string
:param sex: The sex, 'M' or 'F'
:type sex: string
:param lastname: The lastname
:type lastname: string
:param firstname: The firstname
:type firstname: string
:param gender: The gender, 'M' or 'F'
:type gender: string
:param birthdate: The birthdate
:type birthdate: datetime or string
:param birthplace: The birthplace
Expand All @@ -396,11 +396,11 @@ def encode(
:rtype: string
"""

surname_code = encode_surname(surname)
name_code = encode_name(name)
birthdate_code = encode_birthdate(birthdate, sex)
lastname_code = encode_lastname(lastname)
firstname_code = encode_firstname(firstname)
birthdate_code = encode_birthdate(birthdate, gender)
birthplace_code = encode_birthplace(birthplace, birthdate)
code = f"{surname_code}{name_code}{birthdate_code}{birthplace_code}"
code = f"{lastname_code}{firstname_code}{birthdate_code}{birthplace_code}"
cin_code = encode_cin(code)
code = f"{code}{cin_code}"

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

data = {
"code": code,
"surname": match["surname"],
"name": match["name"],
"lastname": match["lastname"],
"firstname": match["firstname"],
"birthdate": match["birthdate"],
"birthdate_year": match["birthdate_year"],
"birthdate_month": match["birthdate_month"],
Expand Down Expand Up @@ -462,9 +462,9 @@ def decode(code: str) -> dict[str, Any]:

if birthdate_day > 40:
birthdate_day -= 40
sex = "F"
gender = "F"
else:
sex = "M"
gender = "M"

current_year = datetime.now().year
current_year_century_prefix = str(current_year)[0:-2]
Expand Down Expand Up @@ -496,7 +496,7 @@ def decode(code: str) -> dict[str, Any]:
data = {
"code": code,
"omocodes": _get_omocodes(code),
"sex": sex,
"gender": gender,
"birthdate": birthdate,
"birthplace": birthplace,
"raw": raw,
Expand Down
36 changes: 18 additions & 18 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_decode_without_omocodes(self):
"""
{
"code": "RSSMRA90A01H501W",
"sex": "M",
"gender": "M",
"birthdate": "1990-01-01T00:00:00",
"birthplace": {
"active": false,
Expand All @@ -116,8 +116,8 @@ def test_decode_without_omocodes(self):
},
"raw": {
"code": "RSSMRA90A01H501W",
"surname": "RSS",
"name": "MRA",
"lastname": "RSS",
"firstname": "MRA",
"birthdate": "90A01",
"birthdate_year": "90",
"birthdate_month": "A",
Expand All @@ -133,7 +133,7 @@ def test_decode_without_omocodes(self):
output_data,
{
"code": "RSSMRA90A01H501W",
"sex": "M",
"gender": "M",
"birthdate": "1990-01-01T00:00:00",
"birthplace": {
"active": False,
Expand All @@ -149,8 +149,8 @@ def test_decode_without_omocodes(self):
},
"raw": {
"code": "RSSMRA90A01H501W",
"surname": "RSS",
"name": "MRA",
"lastname": "RSS",
"firstname": "MRA",
"birthdate": "90A01",
"birthdate_year": "90",
"birthdate_month": "A",
Expand All @@ -170,7 +170,7 @@ def test_decode_without_omocodes_from_command_line(self):
"""
{
"code": "RSSMRA90A01H501W",
"sex": "M",
"gender": "M",
"birthdate": "1990-01-01T00:00:00",
"birthplace": {
"active": false,
Expand All @@ -188,8 +188,8 @@ def test_decode_without_omocodes_from_command_line(self):
},
"raw": {
"code": "RSSMRA90A01H501W",
"surname": "RSS",
"name": "MRA",
"lastname": "RSS",
"firstname": "MRA",
"birthdate": "90A01",
"birthdate_year": "90",
"birthdate_month": "A",
Expand All @@ -206,7 +206,7 @@ def test_decode_without_omocodes_from_command_line(self):
output_data,
{
"code": "RSSMRA90A01H501W",
"sex": "M",
"gender": "M",
"birthdate": "1990-01-01T00:00:00",
"birthplace": {
"active": False,
Expand All @@ -222,8 +222,8 @@ def test_decode_without_omocodes_from_command_line(self):
},
"raw": {
"code": "RSSMRA90A01H501W",
"surname": "RSS",
"name": "MRA",
"lastname": "RSS",
"firstname": "MRA",
"birthdate": "90A01",
"birthdate_year": "90",
"birthdate_month": "A",
Expand Down Expand Up @@ -378,7 +378,7 @@ def test_decode_with_omocodes(self):
"RSSMRAVLALMHRL1F",
"RSSMRAVLALMHRLMX"
],
"sex": "M",
"gender": "M",
"birthdate": "1990-01-01T00:00:00",
"birthplace": {
"active": false,
Expand All @@ -396,8 +396,8 @@ def test_decode_with_omocodes(self):
},
"raw": {
"code": "RSSMRA90A01H501W",
"surname": "RSS",
"name": "MRA",
"lastname": "RSS",
"firstname": "MRA",
"birthdate": "90A01",
"birthdate_year": "90",
"birthdate_month": "A",
Expand Down Expand Up @@ -543,7 +543,7 @@ def test_decode_with_omocodes(self):
"RSSMRAVLALMHRL1F",
"RSSMRAVLALMHRLMX",
],
"sex": "M",
"gender": "M",
"birthdate": "1990-01-01T00:00:00",
"birthplace": {
"active": False,
Expand All @@ -559,8 +559,8 @@ def test_decode_with_omocodes(self):
},
"raw": {
"code": "RSSMRA90A01H501W",
"surname": "RSS",
"name": "MRA",
"lastname": "RSS",
"firstname": "MRA",
"birthdate": "90A01",
"birthdate_year": "90",
"birthdate_month": "A",
Expand Down
Loading

0 comments on commit 2348533

Please sign in to comment.