Skip to content

Commit

Permalink
Refactor de_export.py, extract template functions (#2314)
Browse files Browse the repository at this point in the history
Start moving functionality for amici model code generation to a private
subpackage `amici._codegen`. More to follow.
  • Loading branch information
dweindl authored Feb 26, 2024
1 parent aeb5f34 commit c9b08ac
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 39 deletions.
Empty file.
42 changes: 42 additions & 0 deletions python/sdist/amici/_codegen/template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Functions to apply template substitution to files."""
from pathlib import Path
from string import Template
from typing import Union


class TemplateAmici(Template):
"""
Template format used in AMICI (see :class:`string.Template` for more
details).
:cvar delimiter:
delimiter that identifies template variables
"""

delimiter = "TPL_"


def apply_template(
source_file: Union[str, Path],
target_file: Union[str, Path],
template_data: dict[str, str],
) -> None:
"""
Load source file, apply template substitution as provided in
templateData and save as targetFile.
:param source_file:
relative or absolute path to template file
:param target_file:
relative or absolute path to output file
:param template_data:
template keywords to substitute (key is template
variable without :attr:`TemplateAmici.delimiter`)
"""
with open(source_file) as filein:
src = TemplateAmici(filein.read())
result = src.safe_substitute(template_data)
with open(target_file, "w") as fileout:
fileout.write(result)
40 changes: 1 addition & 39 deletions python/sdist/amici/de_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from dataclasses import dataclass
from itertools import chain
from pathlib import Path
from string import Template
from typing import (
TYPE_CHECKING,
Callable,
Expand All @@ -42,6 +41,7 @@
amiciSwigPath,
splines,
)
from ._codegen.template import apply_template
from .constants import SymbolId
from .cxxcodeprinter import (
AmiciCxxCodePrinter,
Expand Down Expand Up @@ -3995,44 +3995,6 @@ def set_name(self, model_name: str) -> None:
self.model_name = model_name


class TemplateAmici(Template):
"""
Template format used in AMICI (see :class:`string.Template` for more
details).
:cvar delimiter:
delimiter that identifies template variables
"""

delimiter = "TPL_"


def apply_template(
source_file: Union[str, Path],
target_file: Union[str, Path],
template_data: dict[str, str],
) -> None:
"""
Load source file, apply template substitution as provided in
templateData and save as targetFile.
:param source_file:
relative or absolute path to template file
:param target_file:
relative or absolute path to output file
:param template_data:
template keywords to substitute (key is template
variable without :attr:`TemplateAmici.delimiter`)
"""
with open(source_file) as filein:
src = TemplateAmici(filein.read())
result = src.safe_substitute(template_data)
with open(target_file, "w") as fileout:
fileout.write(result)


def get_function_extern_declaration(fun: str, name: str, ode: bool) -> str:
"""
Constructs the extern function declaration for a given function
Expand Down

0 comments on commit c9b08ac

Please sign in to comment.