Skip to content

Commit e6ad11c

Browse files
committed
feat: more robust variable name creation
1 parent 55f4190 commit e6ad11c

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

utils.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
import mathutils
33

44
from enum import Enum, auto
5-
import os
5+
import keyword
66
import re
7-
import shutil
8-
from typing import TextIO, Tuple
7+
from typing import Tuple
98

109
IMAGE_DIR_NAME = "imgs"
1110

@@ -63,13 +62,19 @@ def clean_string(string: str, lower: bool = True) -> str:
6362
string (str): The input string
6463
6564
Returns:
66-
clean_str: The input string with nasty characters converted to underscores
65+
string (str): The input string ready to be used as a variable
6766
"""
6867

6968
if lower:
7069
string = string.lower()
71-
clean_str = re.sub(r"[^a-zA-Z0-9_]", '_', string)
72-
return clean_str
70+
string = re.sub(r"[^a-zA-Z0-9_]", '_', string)
71+
72+
if keyword.iskeyword(string):
73+
string = "_" + string
74+
elif not (string[0].isalpha() or string[0] == '_'):
75+
string = "_" + string
76+
77+
return string
7378

7479
def enum_to_py_str(enum: str) -> str:
7580
"""

0 commit comments

Comments
 (0)