Skip to content

Commit 4483caf

Browse files
Merge pull request #221 from ManuelHu/patch-2
add short-cut for expressions only having a numeric part and unit
2 parents f0fddf1 + a87bd99 commit 4483caf

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/pyg4ometry/gdml/Defines.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,19 @@ def __init__(self, name, expressionString, registry):
2929
self.registry = registry
3030

3131
def eval(self):
32-
# short-cut for expressions only having a numeric path
33-
if _re.match(r"-?[0-9]+(?:\.[0-9]+)$", self.expressionString):
32+
# short-cut for expressions only having a numeric part.
33+
num_re = r"-?[0-9]+(?:\.[0-9]+)?(?:e-?[0-9]{1,2})?"
34+
if _re.match(num_re + "$", self.expressionString):
3435
return float(self.expressionString)
3536

37+
# short-cut for expressions only having a numeric part and a unit.
38+
num_w_unit_re = "(" + num_re + r")(?:\*([a-zA-Z]+))?$"
39+
match_w_unit = _re.match(num_w_unit_re, self.expressionString)
40+
if match_w_unit:
41+
unit = _Units.unit(match_w_unit.group(2))
42+
if unit is not None:
43+
return float(match_w_unit.group(1)) * unit
44+
3645
expressionParser = self.registry.getExpressionParser()
3746
self.parseTree = expressionParser.parse(self.expressionString)
3847
value = expressionParser.evaluate(self.parseTree, self.registry.defineDict)

0 commit comments

Comments
 (0)