fgen.data_models.value#

Data model of a value (e.g. parameter, single return value)

Value#

class Value(definition, unit=None, dynamic_unit=False)[source]#

Bases: object

Data model of a value

This defines the value’s unit, Fortran data type and other metadata. It is the combination of a UnitlessValueDefinition and unit information.

definition: fgen.data_models.unitless_value.UnitlessValue#

Definition of the value’s key information

dynamic_unit: typing.Union[bool, str]#

Whether the unit should be inferred dynamically, rather than statically

If this is True, we will infer the unit using the units of passed pint.Quantity’s. When passing these values to Fortran, the unit will be extracted and passed to Fortran as a string to the attribute whose is_fortran_units_holder is True. If dynamic_unit is True, when retrieving the values from Fortran, the unit will be requested from Fortran too (from the attribute whose is_fortran_units_holder is True) and added to the return value to make a pint.Quantity before returning.

If this is a string, we assume this tells us where to retrieve the unit information from on the Python side (i.e. the string should be valid Python code).

property requires_units: bool#

Whether this value requires units or not

Returns:

True if this value requires units, False otherwise.

unit: typing.Optional[str]#

Unit of the value

The unit must be able to parsed by pint and be present in the pint.UnitRegistry being used by the application (normal rules for pint). Some examples include: “kg”, “1 / month”.

A unit is required for all numeric-types (i.e. integer, real, complex) that don’t have dynamic_unit set. For non-numeric values, no unit is unused.

no_conversion#

no_conversion(inp)[source]#

Identity function, just returns what it is given i.e. performs no conversion

The use case for this is disabling type conversion with cattrs and attrs. In order to get cattrs to not convert stuff, you have to tell the converter a) to prefer attrs converters (i.e. use prefer_attrib_converters=True when creating your cattrs converter) and b) actual supply a converter to your attrs attribute. If you don’t actually want to do any conversion, then you need an identity function (otherwise, cattrs will assume you didn’t think about conversion and inject its own conversion). This lets the validator handle type checking etc., rather than having that check happen in a separate spot (without any context about what is being checked etc.).

Parameters:

inp (typing.TypeVar(T)) – Input

Returns:

typing.TypeVar(T) – inp, unchanged.