Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/correctionlib/highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import json
from collections.abc import Iterator, Mapping
from functools import lru_cache
from numbers import Integral
from typing import TYPE_CHECKING, Any, Callable

Expand Down Expand Up @@ -333,6 +334,11 @@ def __iter__(self) -> Iterator[str]:
return iter(self._base)


@lru_cache(maxsize=64)
def _from_string(data: str) -> correctionlib._core.CorrectionSet:
return correctionlib._core.CorrectionSet.from_string(data)


class CorrectionSet(Mapping[str, Correction]):
"""High-level correction set evaluator object

Expand All @@ -347,7 +353,7 @@ def __init__(self, data: Any):
self._data = data
else:
self._data = data.model_dump_json(exclude_unset=True)
self._base = correctionlib._core.CorrectionSet.from_string(self._data)
self._base = _from_string(self._data)

@classmethod
def from_file(cls, filename: str) -> CorrectionSet:
Expand All @@ -362,7 +368,7 @@ def __getstate__(self) -> dict[str, Any]:

def __setstate__(self, state: dict[str, Any]) -> None:
self._data = state["_data"]
self._base = correctionlib._core.CorrectionSet.from_string(self._data)
self._base = _from_string(self._data)

def _ipython_key_completions_(self) -> list[str]:
return list(self.keys())
Expand Down
Loading