Skip to content

Commit c9b08ac

Browse files
authored
Refactor de_export.py, extract template functions (#2314)
Start moving functionality for amici model code generation to a private subpackage `amici._codegen`. More to follow.
1 parent aeb5f34 commit c9b08ac

File tree

3 files changed

+43
-39
lines changed

3 files changed

+43
-39
lines changed

python/sdist/amici/_codegen/__init__.py

Whitespace-only changes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Functions to apply template substitution to files."""
2+
from pathlib import Path
3+
from string import Template
4+
from typing import Union
5+
6+
7+
class TemplateAmici(Template):
8+
"""
9+
Template format used in AMICI (see :class:`string.Template` for more
10+
details).
11+
12+
:cvar delimiter:
13+
delimiter that identifies template variables
14+
"""
15+
16+
delimiter = "TPL_"
17+
18+
19+
def apply_template(
20+
source_file: Union[str, Path],
21+
target_file: Union[str, Path],
22+
template_data: dict[str, str],
23+
) -> None:
24+
"""
25+
Load source file, apply template substitution as provided in
26+
templateData and save as targetFile.
27+
28+
:param source_file:
29+
relative or absolute path to template file
30+
31+
:param target_file:
32+
relative or absolute path to output file
33+
34+
:param template_data:
35+
template keywords to substitute (key is template
36+
variable without :attr:`TemplateAmici.delimiter`)
37+
"""
38+
with open(source_file) as filein:
39+
src = TemplateAmici(filein.read())
40+
result = src.safe_substitute(template_data)
41+
with open(target_file, "w") as fileout:
42+
fileout.write(result)

python/sdist/amici/de_export.py

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from dataclasses import dataclass
2121
from itertools import chain
2222
from pathlib import Path
23-
from string import Template
2423
from typing import (
2524
TYPE_CHECKING,
2625
Callable,
@@ -42,6 +41,7 @@
4241
amiciSwigPath,
4342
splines,
4443
)
44+
from ._codegen.template import apply_template
4545
from .constants import SymbolId
4646
from .cxxcodeprinter import (
4747
AmiciCxxCodePrinter,
@@ -3995,44 +3995,6 @@ def set_name(self, model_name: str) -> None:
39953995
self.model_name = model_name
39963996

39973997

3998-
class TemplateAmici(Template):
3999-
"""
4000-
Template format used in AMICI (see :class:`string.Template` for more
4001-
details).
4002-
4003-
:cvar delimiter:
4004-
delimiter that identifies template variables
4005-
"""
4006-
4007-
delimiter = "TPL_"
4008-
4009-
4010-
def apply_template(
4011-
source_file: Union[str, Path],
4012-
target_file: Union[str, Path],
4013-
template_data: dict[str, str],
4014-
) -> None:
4015-
"""
4016-
Load source file, apply template substitution as provided in
4017-
templateData and save as targetFile.
4018-
4019-
:param source_file:
4020-
relative or absolute path to template file
4021-
4022-
:param target_file:
4023-
relative or absolute path to output file
4024-
4025-
:param template_data:
4026-
template keywords to substitute (key is template
4027-
variable without :attr:`TemplateAmici.delimiter`)
4028-
"""
4029-
with open(source_file) as filein:
4030-
src = TemplateAmici(filein.read())
4031-
result = src.safe_substitute(template_data)
4032-
with open(target_file, "w") as fileout:
4033-
fileout.write(result)
4034-
4035-
40363998
def get_function_extern_declaration(fun: str, name: str, ode: bool) -> str:
40373999
"""
40384000
Constructs the extern function declaration for a given function

0 commit comments

Comments
 (0)