Spaces:
Sleeping
Sleeping
File size: 2,233 Bytes
2dde43f e78764c |
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 |
from datetime import datetime
import re
def calculate_age(birth_date_str):
"""Calculate age from a birth date string (YYYY-MM-DD)."""
if not birth_date_str:
return None
try:
birth_date = datetime.fromisoformat(birth_date_str)
today = datetime.utcnow()
age = today.year - birth_date.year - ((today.month, today.day) < (birth_date.month, birth_date.day))
return age
except (ValueError, TypeError):
return None
def standardize_language(language):
"""Standardize language codes or text to a consistent format."""
if not language:
return "en"
language = language.lower().strip()
language_map = {
"english": "en",
"french": "fr",
"spanish": "es",
"german": "de"
}
return language_map.get(language, re.sub(r'[^a-z]', '', language)[:2])
def escape_latex_special_chars(text):
"""Escape special characters for LaTeX compatibility."""
if not isinstance(text, str) or not text:
return ""
latex_special_chars = {
'&': r'\&',
'%': r'\%',
'$': r'\$',
'#': r'\#',
'_': r'\_',
'{': r'\{',
'}': r'\}',
'~': r'\textasciitilde{}',
'^': r'\textasciicircum{}',
'\\': r'\textbackslash{}'
}
return ''.join(latex_special_chars.get(c, c) for c in text)
def hyphenate_long_strings(text, max_length=30):
"""Insert hyphens into long strings to prevent LaTeX table overflow."""
if not isinstance(text, str) or not text:
return ""
if len(text) <= max_length:
return text
# Insert a zero-width space (allowing LaTeX to break) every max_length characters
result = []
for i in range(0, len(text), max_length):
chunk = text[i:i + max_length]
result.append(chunk)
return r"\-".join(result)
def format_timestamp(timestamp_str):
"""Format a timestamp string for display in LaTeX."""
if not timestamp_str:
return "N/A"
try:
# Handle ISO format timestamps
dt = datetime.fromisoformat(timestamp_str.replace('Z', '+00:00'))
return dt.strftime("%Y-%m-%d %H:%M")
except (ValueError, TypeError):
return str(timestamp_str) |