From bfda5c4271f518a44d6375ba93822135a2775944 Mon Sep 17 00:00:00 2001 From: Marc Bonnici Date: Mon, 29 Jan 2018 10:46:08 +0000 Subject: [PATCH] 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. --- devlib/utils/misc.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/devlib/utils/misc.py b/devlib/utils/misc.py index ec28c0b..e937d9e 100644 --- a/devlib/utils/misc.py +++ b/devlib/utils/misc.py @@ -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))