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