Skip to content

Commit

Permalink
fixup! Refactor persistence handling: Restructure path building by se…
Browse files Browse the repository at this point in the history
…parating folder and filename construction. This commit will drop the workflow id to allow overwriting and make the path homogenous constructable.

Refactor path construction and update docstrings
  • Loading branch information
fabianliebig committed Nov 25, 2024
1 parent bd00c93 commit bd34ac9
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions benchmarks/persistence/persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,17 @@ def _string_sanitizer(self, string: str) -> str:
def get_path(self, strategy: PathStrategy) -> str:
"""Construct the path of a result object.
Create the path to persist an object based on the storage system used.
Create the path depending on the chosen strategy.
It will make use of the experiment identifier,
the branch, the BayBE-version, the start date without time and
the commit hash.
Args:
strategy: The strategy to construct the path.
Returns:
The path to persist the object. Can be a file name or a folder path.
The path to persist the object. Can be a file name or
a folder path as a string.
"""
separator = "/" if strategy == PathStrategy.Hierarchical else "_"

Expand All @@ -89,7 +93,7 @@ def get_path(self, strategy: PathStrategy) -> str:


class PersistenceHandlingInterface(Protocol):
"""Interface for persisting experiment results."""
"""Interface for interacting with storage."""

def write_object(self, path: PathConstructor, object: dict) -> None:
"""Store a JSON serializable dict according to the path.
Expand All @@ -102,7 +106,7 @@ def write_object(self, path: PathConstructor, object: dict) -> None:

@define
class S3PersistenceHandler:
"""Class for persisting experiment results in an S3 bucket."""
"""Class for persisting objects in an S3 bucket."""

_bucket_name: str = field(validator=instance_of(str), init=False)
"""The name of the S3 bucket where the results are stored."""
Expand All @@ -122,9 +126,8 @@ def write_object(self, path: PathConstructor, object: dict) -> None:
"""Store a JSON serializable dict according to the path.
This method will store an JSON serializable dict in an S3 bucket.
The S3-key of the Java Script Notation Object will be the experiment identifier,
the branch, the BayBE-version, the start datetime, the commit hash and the
workflow run identifier.
The S3-key of the Java Script Notation Object will be created by
the path object.
Args:
path: The path of the object to be persisted
Expand All @@ -142,7 +145,7 @@ def write_object(self, path: PathConstructor, object: dict) -> None:

@define
class LocalFileSystemPersistenceHandler:
"""Class for persisting experiment results in an S3 bucket."""
"""Class for persisting JSON serializable dicts locally."""

folder_path_prefix: str = field(validator=instance_of(str))
"""The prefix of the folder path where the results are stored."""
Expand Down Expand Up @@ -180,14 +183,14 @@ def persister_factory() -> PersistenceHandlingInterface:


def persistence_path_factory(benchmark: Benchmark, result: Result) -> PathConstructor:
"""Create a persistence object.
"""Create a path constructor.
Args:
benchmark: The benchmark for which the result is stored.
result: The result of the benchmark.
Returns:
The persistence object.
The persistence path builder.
"""
benchmark_name = benchmark.name
start_datetime = result.metadata.start_datetime
Expand Down

0 comments on commit bd34ac9

Please sign in to comment.