Skip to content
This repository has been archived by the owner on May 3, 2023. It is now read-only.

Commit

Permalink
feat: use lru cache decorator for values_df loading
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBernstorff committed Nov 16, 2022
1 parent 127f821 commit 4006818
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ torch = "^1.12.1"
transformers = "^4.22.2"
pyarrow = ">=9.0.0,<9.1.0"
psycopmlutils = ">=0.2.4, <0.3.0"
frozendict = "^2.3.4"

[tool.poetry.dev-dependencies]
pytest = ">=7.1.3, <7.1.5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import itertools
from collections.abc import Sequence
from functools import lru_cache
from typing import Any, Callable, Optional, Union

import pandas as pd
from frozendict import frozendict
from pydantic import BaseModel as PydanticBaseModel
from pydantic import Extra

Expand All @@ -14,6 +16,11 @@
from psycop_feature_generation.utils import data_loaders


@lru_cache()
def load_df_with_cache(loader_fn: Callable, kwargs: dict[str, Any]) -> pd.DataFrame:
return loader_fn(**kwargs)


class BaseModel(PydanticBaseModel):
"""."""

Expand Down Expand Up @@ -69,7 +76,10 @@ def resolve_values_df(self, data: dict[str, Any]):
data["values_loader"] = data_loaders.get(data["values_loader"])

if callable(data["values_loader"]):
data["values_df"] = data["values_loader"](**data["loader_kwargs"])
data["values_df"] = load_df_with_cache(
loader_fn=data["values_loader"],
kwargs=frozendict(data["loader_kwargs"]),
)
else:
raise ValueError("values_loader could not be resolved to a callable")

Expand Down

0 comments on commit 4006818

Please sign in to comment.