Skip to content

Commit

Permalink
Fixed squared units not recognized
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-1991 committed Feb 23, 2022
1 parent 51d1e66 commit 3573431
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
8 changes: 6 additions & 2 deletions pyenzyme/enzymeml/tools/unitcreator.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(self):
"second": self.__Seconds,
"seconds": self.__Seconds,
"min": self.__Minutes,
"mins": self.__Minutes,
"minute": self.__Minutes,
"minutes": self.__Minutes,
"h": self.__Hours,
Expand Down Expand Up @@ -74,19 +75,22 @@ def getUnit(self, unit_string, enzmldoc) -> str:
if float(exponent) > 0:
if abs(float(exponent)) > 1:
nominator.append(
pre_unit + f"**{exponent}"
pre_unit + f"^{exponent[1::]}"
)
if abs(float(exponent)) == 1:
nominator.append(pre_unit)
else:
if abs(float(exponent)) > 1:
denominator.append(
pre_unit + f"**{exponent}"
pre_unit + f"^{exponent[1::]}"
)
if abs(float(exponent)) == 1:
denominator.append(pre_unit)

# Reformat unit string to a convenient format
if not nominator:
nominator = ["1"]

if denominator:
name = " / ".join([
" ".join(nominator),
Expand Down
11 changes: 7 additions & 4 deletions pyenzyme/enzymeml/tools/unitparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def parse(self, exp_string):

# reformat string
exp_string = self.__exponentString(exp_string)

# split by exponents
regex = r"([a-zA-Z]*)([-+][\d]*)"
regex = regex.replace(' ', '')
Expand All @@ -44,7 +45,10 @@ def parse(self, exp_string):
self.__getPrefix(
tup[0],
tup[-1]
) for tup in unit_tup]
)
for tup in unit_tup
if tup[0]
]

def getExponentString(self, string):

Expand All @@ -64,9 +68,8 @@ def getExponentString(self, string):
)

def __reformatString(self, string, pre):
regex = r"([a-zA-z]*)([-+]?\d*)"
regex = r"(\w*)[-+|\^]?(\d*)"
groups = re.findall(regex, string)

exp_string = ""

for unit, exponent in groups:
Expand All @@ -85,7 +88,7 @@ def __reformatString(self, string, pre):
return exp_string

def __exponentString(self, string):

string = [st.strip() for st in string.split('/')]

if len(string) == 2:
Expand Down

0 comments on commit 3573431

Please sign in to comment.