Skip to content

Commit

Permalink
Draft of policy functions class.
Browse files Browse the repository at this point in the history
  • Loading branch information
MImmesberger committed Jan 24, 2025
1 parent ff4e1b2 commit 70b446c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/_gettsim/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def set_array_backend(backend: str):
GEP_01_CHARACTER_LIMIT_USER_FACING_COLUMNS = 20
GEP_01_CHARACTER_LIMIT_OTHER_COLUMNS = 32

QUALIFIED_NAME_DELIMITER = "__"

# List of paths to internal functions.
# If a path is a directory, all Python files are recursively collected from that folder.
PATHS_TO_INTERNAL_FUNCTIONS = [
Expand Down
22 changes: 14 additions & 8 deletions src/_gettsim/functions/policy_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import numpy

from _gettsim.config import QUALIFIED_NAME_DELIMITER

T = TypeVar("T")


Expand Down Expand Up @@ -39,8 +41,7 @@ def __init__( # noqa: PLR0913
self,
function: Callable,
*,
module_name: str = "",
function_name: str | None = None,
qualified_name: str,
start_date: date | None = None,
end_date: date | None = None,
params_key_for_rounding: str | None = None,
Expand All @@ -55,13 +56,8 @@ def __init__( # noqa: PLR0913
self.function = (
function if self.skip_vectorization else _vectorize_func(function)
)
self.module_name = module_name

self.name_in_dag: str = _first_not_none(
function_name,
info.get("name_in_dag"),
function.__name__,
)
self.qualified_name = qualified_name

self.start_date: date = _first_not_none(
start_date,
Expand Down Expand Up @@ -103,6 +99,16 @@ def original_function_name(self) -> str:
"""The name of the wrapped function."""
return self.function.__name__

@property
def simple_name(self) -> str:
"""The name of the wrapped function."""
return self.qualified_name.split(QUALIFIED_NAME_DELIMITER)[-1]

@property
def branches_of_namespace(self) -> list[str]:
"""The branches of the wrapped function."""
return self.qualified_name.split(QUALIFIED_NAME_DELIMITER)[:-1]

def is_active_at_date(self, date: date) -> bool:
"""Check if the function is active at a given date."""
return self.start_date <= date <= self.end_date
Expand Down

0 comments on commit 70b446c

Please sign in to comment.