From ef97a5f4a077fb00e2725ec79b7c7bfcf778a2e8 Mon Sep 17 00:00:00 2001 From: Chuck McCallum Date: Tue, 26 Nov 2024 13:30:38 -0500 Subject: [PATCH] column names from user could start with numbers --- dp_wizard/utils/code_generators/__init__.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/dp_wizard/utils/code_generators/__init__.py b/dp_wizard/utils/code_generators/__init__.py index 7a0e8d1..de516c5 100644 --- a/dp_wizard/utils/code_generators/__init__.py +++ b/dp_wizard/utils/code_generators/__init__.py @@ -244,8 +244,14 @@ def _snake_case(name: str): """ >>> _snake_case("HW GRADE") 'hw_grade' + >>> _snake_case("123") + '_123' """ - return re.sub(r"\W+", "_", name.lower()) + snake = re.sub(r"\W+", "_", name.lower()) + # TODO: More validation in UI so we don't get zero-length strings. + if snake == "" or not re.match(r"[a-z]", snake[0]): + snake = f"_{snake}" + return snake def _make_imports():