From 15c1460fae10519df0a1d02888a8028ae7a797cf Mon Sep 17 00:00:00 2001 From: Tanner Prestegard <tanner.prestegard@ligo.org> Date: Mon, 16 Apr 2018 08:02:30 -0500 Subject: [PATCH] adding some utility functions which might be helpful for managing superevent IDs --- gracedb/core/utils.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 gracedb/core/utils.py diff --git a/gracedb/core/utils.py b/gracedb/core/utils.py new file mode 100644 index 000000000..a81b6673a --- /dev/null +++ b/gracedb/core/utils.py @@ -0,0 +1,33 @@ +import string + +# Get lowercase alphabet as string +ALPHABET = string.ascii_lowercase +BASE = len(ALPHABET) + +def int_to_letters(num, positive_only=True): + """ + + Enumeration starts at 1 (i.e., 1 => 'a') + """ + + # Argument checking + if not isinstance(num, int): + raise TypeError("Input 'num' is not an int") + if (positive_only and num <= 0): + raise ValueError(("Input 'num' is non-positive, but the positive_only " + "flag is set")) + + out_str = '' + while (num > BASE): + r = num % BASE + num /= BASE + out_str = ALPHABET[r-1] + out_str + out_str = ALPHABET[num-1] + out_str + return out_str + +def letters_to_int(letters): + # TODO: remove assert statement! + assert isinstance(letters, str), "letters is not a string" + + letters = letters.lower() + pass -- GitLab