Skip to content

Commit 053fafd

Browse files
committed
Up to writing wrapper
1 parent 4b6f728 commit 053fafd

6 files changed

Lines changed: 167 additions & 4 deletions

File tree

meson.build

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,12 @@ if pyprojectwheelbuild_enabled
8585
'src/example_fgen_basic/error_v/error_v.py',
8686
'src/example_fgen_basic/error_v/passing.py',
8787
'src/example_fgen_basic/exceptions.py',
88+
'src/example_fgen_basic/get_square_root.py',
8889
'src/example_fgen_basic/get_wavelength.py',
8990
'src/example_fgen_basic/pyfgen_runtime/__init__.py',
9091
'src/example_fgen_basic/pyfgen_runtime/exceptions.py',
92+
'src/example_fgen_basic/result/__init__.py',
93+
'src/example_fgen_basic/result/result_dp.py',
9194
'src/example_fgen_basic/typing.py',
9295
)
9396

src/example_fgen_basic/error_v/creation.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ def create_error(inv: int) -> ErrorV:
5454
# Initialise the result from the received index
5555
res = ErrorV.from_instance_index(instance_index)
5656

57-
# Tell Fortran to finalise the object on the Fortran side
58-
# (all data has been copied to Python now)
59-
m_error_v_w.finalise_instance(instance_index)
60-
6157
return res
6258

6359

src/example_fgen_basic/error_v/error_v.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ def from_instance_index(cls, instance_index: int) -> ErrorV:
5959

6060
res = cls(code=code, message=message)
6161

62+
# Tell Fortran to finalise the object on the Fortran side
63+
# (all data has been copied to Python now)
64+
m_error_v_w.finalise_instance(instance_index)
65+
6266
return res
6367

6468
def build_fortran_instance(self) -> int:
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
Get square root of a number
3+
"""
4+
5+
from __future__ import annotations
6+
7+
from example_fgen_basic.pyfgen_runtime.exceptions import (
8+
CompiledExtensionNotFoundError,
9+
FortranError,
10+
)
11+
from example_fgen_basic.result import ResultDP
12+
13+
try:
14+
from example_fgen_basic._lib import m_get_square_root_w # type: ignore
15+
except (ModuleNotFoundError, ImportError) as exc: # pragma: no cover
16+
raise CompiledExtensionNotFoundError(
17+
"example_fgen_basic._lib.m_get_square_root_w"
18+
) from exc
19+
20+
try:
21+
from example_fgen_basic._lib import m_result_dp_w # type: ignore
22+
except (ModuleNotFoundError, ImportError) as exc: # pragma: no cover
23+
raise CompiledExtensionNotFoundError(
24+
"example_fgen_basic._lib.m_result_dp_w"
25+
) from exc
26+
27+
28+
def get_square_root(inv: float) -> float:
29+
"""
30+
Get square root
31+
32+
Parameters
33+
----------
34+
inv
35+
Value for which to get the square root
36+
37+
Returns
38+
-------
39+
:
40+
Square root of `inv`
41+
42+
Raises
43+
------
44+
FortranError
45+
`inv` is negative
46+
47+
TODO: use a more specific error
48+
"""
49+
result_instance_index: int = m_get_square_root_w.get_wavelength(inv)
50+
51+
result = ResultDP.from_instance_index(result_instance_index)
52+
53+
if result.is_error:
54+
# TODO: be more specific
55+
raise FortranError(result.error_v.message)
56+
57+
res = result.data_v
58+
59+
m_result_dp_w.finalise_instance(result_instance_index)
60+
61+
return res
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
Definition of result values
3+
"""
4+
5+
from example_fgen_basic.result.result_dp import ResultDP
6+
7+
__all__ = ["ResultDP"]
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)