Spaces:
Sleeping
Sleeping
Create utils/helpers.py
Browse files- utils/helpers.py +27 -0
utils/helpers.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
import re
|
| 3 |
+
|
| 4 |
+
def calculate_age(birth_date_str):
|
| 5 |
+
"""Calculate age from a birth date string (YYYY-MM-DD)."""
|
| 6 |
+
if not birth_date_str:
|
| 7 |
+
return None
|
| 8 |
+
try:
|
| 9 |
+
birth_date = datetime.fromisoformat(birth_date_str)
|
| 10 |
+
today = datetime.utcnow()
|
| 11 |
+
age = today.year - birth_date.year - ((today.month, today.day) < (birth_date.month, birth_date.day))
|
| 12 |
+
return age
|
| 13 |
+
except (ValueError, TypeError):
|
| 14 |
+
return None
|
| 15 |
+
|
| 16 |
+
def standardize_language(language):
|
| 17 |
+
"""Standardize language codes or text to a consistent format."""
|
| 18 |
+
if not language:
|
| 19 |
+
return "en"
|
| 20 |
+
language = language.lower().strip()
|
| 21 |
+
language_map = {
|
| 22 |
+
"english": "en",
|
| 23 |
+
"french": "fr",
|
| 24 |
+
"spanish": "es",
|
| 25 |
+
"german": "de"
|
| 26 |
+
}
|
| 27 |
+
return language_map.get(language, re.sub(r'[^a-z]', '', language)[:2])
|