1
0
mirror of https://github.com/ARM-software/devlib.git synced 2025-01-31 10:10:46 +00:00

utils/misc: Check if identifier starts with a number

Now ensures that the given text does not start with a digit and if so
prefix an underscore to ensure a valid python identifier.
This commit is contained in:
Marc Bonnici 2018-01-29 10:46:08 +00:00 committed by setrofim
parent d1b08f6df6
commit bfda5c4271

View File

@ -523,7 +523,9 @@ TRANS_TABLE = string.maketrans(BAD_CHARS, '_' * len(BAD_CHARS))
def to_identifier(text):
"""Converts text to a valid Python identifier by replacing all
whitespace and punctuation."""
whitespace and punctuation and adding a prefix if starting with a digit"""
if text[:1].isdigit():
text = '_' + text
return re.sub('_+', '_', text.translate(TRANS_TABLE))