SIENTIAPDE-1579: Switched from replace logic to validation + mapping

This commit is contained in:
Kou Kinoshita
2026-02-18 18:57:41 -03:00
parent e7e851dbb5
commit 14f3fbadbe
5 changed files with 105 additions and 34 deletions

View File

@@ -17,14 +17,35 @@ NORMALIZATION = 'Normalization'
FEATURE_CREATION = 'Feature Creation'
LAG_CREATION = 'Lag Creation'
# Allowed frontend date formats and their strftime equivalents (single source of truth)
FRONTEND_DATE_FORMAT_TO_STRFTIME = {
'dd/MM/yyyy HH:mm:ss': '%d/%m/%Y %H:%M:%S',
'MM/dd/yyyy HH:mm:ss': '%m/%d/%Y %H:%M:%S',
'yyyy/MM/dd HH:mm:ss': '%Y/%m/%d %H:%M:%S',
'dd-MM-yyyy HH:mm:ss': '%d-%m-%Y %H:%M:%S',
'MM-dd-yyyy HH:mm:ss': '%m-%d-%Y %H:%M:%S',
'yyyy-MM-dd HH:mm:ss': '%Y-%m-%d %H:%M:%S',
}
ALLOWED_FRONTEND_DATE_FORMATS = frozenset(FRONTEND_DATE_FORMAT_TO_STRFTIME.keys())
def validate_frontend_date_format(fmt: str | None) -> None:
"""Raise ValueError if fmt is set and not one of the allowed frontend date formats."""
if not fmt or not fmt.strip():
return
if fmt not in ALLOWED_FRONTEND_DATE_FORMATS:
allowed = ', '.join(sorted(ALLOWED_FRONTEND_DATE_FORMATS))
raise ValueError(
f'Invalid date_format "{fmt}". Allowed formats: {allowed}'
)
def _frontend_date_format_to_strftime(fmt: str | None) -> str | None:
"""Convert front-end date format (e.g. dd/MM/yyyy HH:mm:ss) to Python strftime."""
"""Convert front-end date format to Python strftime. Validates format; returns None for empty."""
if not fmt:
return None
out = fmt.replace('yyyy', '%Y').replace('MM', '%m').replace('dd', '%d')
out = out.replace('HH', '%H').replace('mm', '%M').replace('ss', '%S')
return out
validate_frontend_date_format(fmt)
return FRONTEND_DATE_FORMAT_TO_STRFTIME[fmt]
class LinearRegressionModel(BaseEstimator, TransformerMixin):