-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
34 lines (27 loc) · 1.05 KB
/
utils.py
File metadata and controls
34 lines (27 loc) · 1.05 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
import zipcodes as zcode
from typing import Dict
def get_country_by_zipcode(zipcode):
"""Fetch country based on zipcode."""
results = zcode.matching(str(zipcode))
if not results:
return None
return results[0].get("country", None)
def translate_gender(gender):
"""Translate gender shorthand to full form."""
gender_map = {"F": "Female", "M": "Male"}
if gender not in gender_map:
raise ValueError(f"Invalid gender value: {gender}")
return gender_map[gender]
def format_user_profile(user_profile: Dict) -> str:
"""Formats a user profile dictionary into a readable string format."""
profile_lines = []
for key, value in user_profile.items():
if key == "gender":
value = translate_gender(value)
elif key == "zip_code":
country = get_country_by_zipcode(value)
if country is not None:
profile_lines.append(f"Country: {country}")
continue
profile_lines.append(f"{key.capitalize()}: {value}")
return "\n".join(profile_lines)