Skip to content

Commit

Permalink
Merge pull request HelgeGehring#39 from fbeutel/parse_alphanumeric
Browse files Browse the repository at this point in the history
Add parse_alphanumeric as inverse of id_to_alphanumeric
  • Loading branch information
HelgeGehring authored Dec 10, 2020
2 parents 1c875f1 + bedfc24 commit f3b58e5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
13 changes: 13 additions & 0 deletions gdshelpers/helpers/small.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import string
import numpy as np
import numpy.linalg as linalg
import re


def raith_eline_dosefactor_to_datatype(dose_factor):
Expand Down Expand Up @@ -47,6 +48,18 @@ def id_to_alphanumeric(column, row):
return int_to_alphabet(row) + str(int(column))


def alphanumeric_to_id(text):
"""
Do the reverse of `id_to_alphanumeric`.
:return: (column, row) tuple.
"""
regex = "([A-Z]*)([0-9]*)"
m = re.match(regex, text, re.I)
letters, number = m.group(1), int(m.group(2))
letter_num = sum((ord(letter) - ord('A') + 1) * 26 ** i for i, letter in enumerate(letters[::-1]))
return number, letter_num - 1


def normalize_phase(phase, zero_to_two_pi=False):
"""
Normalize a phase to be within +/- pi.
Expand Down
7 changes: 7 additions & 0 deletions gdshelpers/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy.testing as npt

from gdshelpers.helpers import int_to_alphabet, id_to_alphanumeric, find_line_intersection
from gdshelpers.helpers.small import alphanumeric_to_id


class HelpersTestCase(unittest.TestCase):
Expand All @@ -15,6 +16,12 @@ def test_int_to_alphabet(self):
def test_id_to_alphanumeric(self):
self.assertEqual(id_to_alphanumeric(4, 26 + 25), 'AZ4')

def test_alphanumeric_to_id(self):
self.assertEqual((4, 26 + 25), alphanumeric_to_id('AZ4'))

for pair in [(0, 0), (0, 10), (0, 100), (100, 0), (100, 100)]:
self.assertEqual(pair, alphanumeric_to_id(id_to_alphanumeric(*pair)))

def test_find_line_intersection(self):
test_intersection = find_line_intersection(np.array((2, 0)), np.pi / 2, np.array((0, 1)), 0)
npt.assert_almost_equal(test_intersection[0], (2, 1))
Expand Down

0 comments on commit f3b58e5

Please sign in to comment.