Skip to content

refactored parse_dim_arg #923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions _unittest/test_00_GeometryOperators.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def test_parse_dim_arg(self):
assert go.parse_dim_arg("-3.4e-2") == -3.4e-2
assert abs(go.parse_dim_arg("180deg") - math.pi) < tol
assert go.parse_dim_arg("1.57rad") == 1.57
assert go.parse_dim_arg("3km", ) == 3000.0
assert go.parse_dim_arg("1m_per_h") == 3600.0

def test_cs_plane_str(self):
assert go.cs_plane_to_axis_str(PLANE.XY) == "Z"
Expand Down
25 changes: 25 additions & 0 deletions pyaedt/generic/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,31 @@ def unit_converter(value, unit_system="Length", input_units="meter", output_unit
return value


def scale_units(scale_to_unit):
"""Find the scale_to_unit into main system unit.

Parameters
----------
scale_to_unit : str
Unit to Scale.

Returns
-------
float
Return the scaling factor if any.
"""
sunit = 1.0
for el, val in AEDT_UNITS.items():
for unit, scale_val in val.items():
if scale_to_unit.lower() == unit.lower():
sunit = scale_val
break
else:
continue
break
return sunit


AEDT_UNITS = {
"AngularSpeed": {
"deg_per_hr": HOUR2SEC * DEG2RAD,
Expand Down
48 changes: 6 additions & 42 deletions pyaedt/modeler/GeometryOperators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from pyaedt.generic.constants import AXIS
from pyaedt.generic.constants import PLANE
from pyaedt.generic.constants import scale_units
from pyaedt.generic.constants import SWEEPDRAFT
from pyaedt.generic.general_methods import pyaedt_function_handler

Expand Down Expand Up @@ -72,46 +73,17 @@ def parse_dim_arg(string, scale_to_unit=None, variable_manager=None):
2.0

"""
scaling = {
"m": 1.0,
"meter": 1.0,
"meters": 1.0,
"dm": 0.1,
"cm": 1e-2,
"mm": 1e-3,
"um": 1e-6,
"nm": 1e-9,
"in": 2.54e-2,
"mil": 2.54e-5,
"uin": 2.54e-8,
"ft": 3.048e-1,
"s": 1.0,
"sec": 1.0,
"ms": 1e-3,
"us": 1e-6,
"ns": 1e-9,
"Hz": 1.0,
"kHz": 1e3,
"MHz": 1e6,
"GHz": 1e9,
"THz": 1e12,
}

if type(string) is not str:
try:
return float(string)
except ValueError: # pragma: no cover
raise TypeError("Input argument is not string nor number")

sunit = 1.0
if scale_to_unit:
try:
sunit = scaling[scale_to_unit]
except KeyError as e: # pragma: no cover
raise e
else:
sunit = 1.0
sunit = scale_units(scale_to_unit)

pattern = r"(?P<number>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)\s*(?P<unit>[a-zA-Z]*)"
pattern = r"(?P<number>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)\s*(?P<unit>[a-z_A-Z]*)"
m = re.search(pattern, string)
if m:
if m.group(0) != string:
Expand All @@ -124,17 +96,9 @@ def parse_dim_arg(string, scale_to_unit=None, variable_manager=None):
return string
elif not m.group("unit"):
return float(m.group("number"))
elif m.group("unit") == "deg":
return GeometryOperators.deg2rad(float(m.group("number")))
elif m.group("unit") == "rad":
return float(m.group("number"))
else:
try:
scaling_factor = scaling[m.group("unit")]
except KeyError as e: # pragma: no cover
raise e
else:
return float(m.group("number")) * scaling_factor / sunit
scaling_factor = scale_units(m.group("unit"))
return float(m.group("number")) * scaling_factor / sunit
else: # pragma: no cover
raise TypeError("String is no number")

Expand Down