|
| 1 | +""" |
| 2 | +Python equivalent of the Fortran `ResultDP` class [TODO: x-refs] |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +from attrs import define |
| 8 | + |
| 9 | +from example_fgen_basic.error_v import ErrorV |
| 10 | +from example_fgen_basic.pyfgen_runtime.exceptions import CompiledExtensionNotFoundError |
| 11 | + |
| 12 | +try: |
| 13 | + from example_fgen_basic._lib import ( # type: ignore |
| 14 | + m_result_dp_w, |
| 15 | + ) |
| 16 | +except (ModuleNotFoundError, ImportError) as exc: # pragma: no cover |
| 17 | + raise CompiledExtensionNotFoundError( |
| 18 | + "example_fgen_basic._lib.m_result_dp_w" |
| 19 | + ) from exc |
| 20 | + |
| 21 | + |
| 22 | +@define |
| 23 | +class ResultDP: |
| 24 | + """ |
| 25 | + Result type that can hold double precision real values |
| 26 | + """ |
| 27 | + |
| 28 | + # TODO: add validation that one of data_v and error_v is provided but not both |
| 29 | + |
| 30 | + # data_v: np.Float64 |
| 31 | + data_v: float |
| 32 | + """Data""" |
| 33 | + |
| 34 | + error_v: ErrorV |
| 35 | + """Error""" |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def from_instance_index(cls, instance_index: int) -> ErrorV: |
| 39 | + """ |
| 40 | + Initialise from an instance index received from Fortran |
| 41 | +
|
| 42 | + Parameters |
| 43 | + ---------- |
| 44 | + instance_index |
| 45 | + Instance index received from Fortran |
| 46 | +
|
| 47 | + Returns |
| 48 | + ------- |
| 49 | + : |
| 50 | + Initialised index |
| 51 | + """ |
| 52 | + # Different wrapping strategies are needed |
| 53 | + |
| 54 | + # Integer is very simple |
| 55 | + if m_result_dp_w.data_v_is_set(instance_index): |
| 56 | + data_v = m_result_dp_w.get_data_v(instance_index) |
| 57 | + |
| 58 | + else: |
| 59 | + data_v = None |
| 60 | + |
| 61 | + # Error type requires derived type handling |
| 62 | + if m_result_dp_w.error_v_is_set(instance_index): |
| 63 | + error_v_instance_index: int = m_result_dp_w.get_error_v(instance_index) |
| 64 | + |
| 65 | + # Initialise the result from the received index |
| 66 | + error_v = ErrorV.from_instance_index(error_v_instance_index) |
| 67 | + |
| 68 | + else: |
| 69 | + error_v = None |
| 70 | + |
| 71 | + res = cls(data_v=data_v, error_v=error_v) |
| 72 | + |
| 73 | + return res |
| 74 | + |
| 75 | + def build_fortran_instance(self) -> int: |
| 76 | + """ |
| 77 | + Build an instance equivalent to `self` on the Fortran side |
| 78 | +
|
| 79 | + Intended for use mainly by wrapping functions. |
| 80 | + Most users should not need to use this method directly. |
| 81 | +
|
| 82 | + Returns |
| 83 | + ------- |
| 84 | + : |
| 85 | + Instance index of the object which has been created on the Fortran side |
| 86 | + """ |
| 87 | + raise NotImplementedError |
| 88 | + # instance_index: int = m_error_v_w.build_instance( |
| 89 | + # code=self.code, message=self.message |
| 90 | + # ) |
| 91 | + # |
| 92 | + # return instance_index |
0 commit comments