Skip to content

Commit

Permalink
[Units] Refactor setter to use a loop
Browse files Browse the repository at this point in the history
Rather than checking each value individually with try/except, use the
ito method and try to convert within a loop. This simplifies testing for
the error condition and looks a little cleaner.
  • Loading branch information
bryanwweber authored and speth committed Apr 25, 2023
1 parent c178c7c commit a832a71
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 34 deletions.
46 changes: 24 additions & 22 deletions interfaces/cython/SConscript
Expand Up @@ -172,17 +172,18 @@ setter_2_template = Template("""
@${name}.setter
def ${name}(self, value):
try:
${n0} = value[0].to(${u0}) if value[0] is not None else self.${n0}
${n1} = value[1].to(${u1}) if value[1] is not None else self.${n1}
except AttributeError as e:
# The 'to' attribute missing means this isn't a pint Quantity
if "'to'" in str(e):
raise CanteraError(
f"Values ({value}) must be instances of pint.Quantity classes"
) from None
else:
raise
${n0} = value[0] if value[0] is not None else self.${n0}
${n1} = value[1] if value[1] is not None else self.${n1}
for val, unit in ((${n0}, ${u0}), (${n1}, ${u1})):
try:
val.ito(unit)
except AttributeError as e:
if "'ito'" in str(e):
raise CanteraError(
f"Value {val!r} must be an instance of a pint.Quantity class"
) from None
else:
raise # pragma: no cover
self._phase.${name} = ${n0}.magnitude, ${n1}.magnitude
""")

Expand All @@ -209,17 +210,18 @@ setter_3_template = Template("""
@${name}.setter
def ${name}(self, value):
try:
${n0} = value[0].to(${u0}) if value[0] is not None else self.${n0}
${n1} = value[1].to(${u1}) if value[1] is not None else self.${n1}
except AttributeError as e:
# The 'to' attribute missing means this isn't a pint Quantity
if "'to'" in str(e):
raise CanteraError(
f"Values ({value}) must be instances of pint.Quantity classes"
) from None
else:
raise
${n0} = value[0] if value[0] is not None else self.${n0}
${n1} = value[1] if value[1] is not None else self.${n1}
for val, unit in ((${n0}, ${u0}), (${n1}, ${u1})):
try:
val.ito(unit)
except AttributeError as e:
if "'ito'" in str(e):
raise CanteraError(
f"Value {val!r} must be an instance of a pint.Quantity class"
) from None
else:
raise # pragma: no cover
if value[2] is not None:
try:
${n2} = value[2].to(${u2}).magnitude
Expand Down
28 changes: 16 additions & 12 deletions interfaces/cython/cantera/with_units/solution.py.in
Expand Up @@ -38,10 +38,13 @@ class PureFluid:
if value is not None:
try:
Q = value.to("dimensionless").magnitude
except AttributeError:
raise CanteraError(
"Values must be instances of pint.Quantity classes"
) from None
except AttributeError as e:
if "'to'" in str(e):
raise CanteraError(
f"Value {value!r} must be an instance of a pint.Quantity class"
) from None
else:
raise # pragma: no cover
else:
Q = self.Q.magnitude
self._phase.Q = Q
Expand All @@ -55,17 +58,18 @@ class PureFluid:

@TPQ.setter
def TPQ(self, value):
msg = "Value {value!r} must be an instance of a pint.Quantity class"
T = value[0] if value[0] is not None else self.T
P = value[1] if value[1] is not None else self.P
Q = value[2] if value[2] is not None else self.Q
try:
T = T.to("K")
P = P.to("Pa")
Q = Q.to("dimensionless")
except AttributeError:
raise CanteraError(
"Values must be instances of pint.Quantity classes"
) from None
for val, unit in ((T, "K"), (P, "Pa"), (Q, "dimensionless")):
try:
val.ito(unit)
except AttributeError as e:
if "'ito'" in str(e):
raise CanteraError(msg.format(value=val)) from None
else:
raise # pragma: no cover
self._phase.TPQ = T.magnitude, P.magnitude, Q.magnitude

@purefluid_properties@
Expand Down

0 comments on commit a832a71

Please sign in to comment.