-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreation.py
More file actions
93 lines (72 loc) · 2.61 KB
/
creation.py
File metadata and controls
93 lines (72 loc) · 2.61 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
"""
Wrappers of `m_error_v_creation` [TODO think about naming and x-referencing]
At the moment, all written by hand.
We will auto-generate this in future.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
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_error_v_w,
)
except (ModuleNotFoundError, ImportError) as exc: # pragma: no cover
raise CompiledExtensionNotFoundError("example_fgen_basic._lib.m_error_v_w") from exc
try:
from example_fgen_basic._lib import m_error_v_creation_w
except (ModuleNotFoundError, ImportError) as exc: # pragma: no cover
raise CompiledExtensionNotFoundError(
"example_fgen_basic._lib.m_error_v_creation_w"
) from exc
if TYPE_CHECKING:
from example_fgen_basic.typing import NP_ARRAY_OF_INT
def create_error(inv: int) -> ErrorV:
"""
Create an error
Parameters
----------
inv
Input value
If odd, the error code is
[NO_ERROR_CODE][example_fgen_basic.error_v.error_v.NO_ERROR_CODE].
If even, the error code is 1.
If a negative number is supplied, the error code is 2.
Returns
-------
:
Created error
"""
# Get the result, but receiving an instance index rather than the object itself
instance_index: int = m_error_v_creation_w.create_error(inv)
# Initialise the result from the received index
res = ErrorV.from_instance_index(instance_index)
# Tell Fortran to finalise the object on the Fortran side
# (all data has been copied to Python now)
m_error_v_w.finalise_instance(instance_index)
return res
def create_errors(invs: NP_ARRAY_OF_INT) -> tuple[ErrorV, ...]:
"""
Create a number of errors
Parameters
----------
invs
Input values from which to create errors
For each value in `invs`,
if the value is even, an error is created,
if the value is odd, an error with a no error code is created.
Returns
-------
:
Created errors
"""
# Get the result, but receiving an instance index rather than the object itself
instance_indexes: NP_ARRAY_OF_INT = m_error_v_creation_w.create_errors(
invs, len(invs)
)
# Initialise the result from the received index
res = tuple(ErrorV.from_instance_index(i) for i in instance_indexes)
# Tell Fortran to finalise the object on the Fortran side
# (all data has been copied to Python now)
m_error_v_w.finalise_instances(instance_indexes)
return res