From 0ac9fd79f9abde77a8b4b45756b5fa30ea296e05 Mon Sep 17 00:00:00 2001 From: fbeutel Date: Thu, 10 Dec 2020 16:16:46 +0100 Subject: [PATCH] Apply suggestions from code review Add Helge's review suggestions Co-authored-by: Helge Gehring --- gdshelpers/helpers/small.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/gdshelpers/helpers/small.py b/gdshelpers/helpers/small.py index 073b5fb..9196148 100644 --- a/gdshelpers/helpers/small.py +++ b/gdshelpers/helpers/small.py @@ -48,17 +48,15 @@ def id_to_alphanumeric(column, row): return int_to_alphabet(row) + str(int(column)) -def parse_alphanumeric(text): +def alphanumeric_to_id(text): """ Do the reverse of `id_to_alphanumeric`. - Returns the (column, row) tuple. + :return: (column, row) tuple. """ - regex = "([A-Z]*)[^0-9]*([0-9]*)" - m = re.match(regex, text.strip(), re.I) + regex = "([A-Z]*)([0-9]*)" + m = re.match(regex, text, re.I) letters, number = m.group(1), int(m.group(2)) - letter_num = 0 - for i in range(len(letters)): - letter_num += (ord(letters[i]) - 64) * (26 ** (len(letters) - i - 1)) + letter_num = sum((ord(letter) - ord('A') + 1) * 26 ** i for i, letter in enumerate(letters[::-1])) return number, letter_num - 1