Allow units to be included in the value field

This commit is contained in:
Lukas Klass 2020-10-23 13:27:59 +02:00
parent c3a32ed7af
commit dac889a1fd
2 changed files with 14 additions and 3 deletions

View File

@ -11,9 +11,14 @@ Each parameter of the configuration is defined by a XML-tag which is indicated b
A parameter can contain multiple attributes which are defined as key-value-pairs and some of them may be optional.
All attribute values must be quoted as in the example below. The required types shown in the documentation refer to the unquoted value.
Most numeric attributes require a unit which has to be defined as a second attribute with the suffix *_unit* which is also shown in the example below.
Most numeric attributes require a unit which can be either included in the value or defined as a second attribute with the suffix *_unit*.
The documentation uses the latter case for clarity. Both cases are shown as examples below.
All astropy units defined `here <https://docs.astropy.org/en/stable/units/>`_ can be used.
.. code-block:: xml
<tag_name val="10 s"/>
.. code-block:: xml
<tag_name val="10" val_unit="s"/>

View File

@ -79,8 +79,14 @@ class Entry(object):
try:
self.__setattr__(name, float(attr) * unit)
except ValueError:
return "Expected parameter '" + name + "' with unit '" + unit.to_string() + \
"' but got no unit and cannot convert '" + attr + "' to a numeric value."
try:
self.__setattr__(name, u.Quantity(attr))
mes = self.check_quantity(name, unit, use_default)
if mes is not None:
return mes
except [ValueError, TypeError]:
return "Expected parameter '" + name + "' with unit '" + unit.to_string() + \
"' but got no unit and cannot convert '" + attr + "' to a numeric value."
else:
return "Expected parameter '" + name + "' with unit '" + unit.to_string() + "' but got no unit."
elif not attr.unit.is_equivalent(unit):