Skip to content

Commit

Permalink
FIX: Apply doesn't work when path ends with /
Browse files Browse the repository at this point in the history
  • Loading branch information
d-krupke committed Apr 4, 2024
1 parent b7fc43c commit ad038c3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ Finally, add ``.gitattributes`` to git via
Version History
===============

- **2.4.1** Fixes bug when path ends with `/`.
- **2.4.0** Removing information on installed packages due to deprecated ``pkg_resources``. New apply-function.
- **2.2.2** Fixing problem with Jupyter notebooks, because they may not have a ``__file__`` attribute.
- **2.2.1** Should be able to deal with corrupt zip files now.
Expand Down
10 changes: 5 additions & 5 deletions src/algbench/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import typing
import os
from contextlib import ExitStack, redirect_stderr, redirect_stdout

from pathlib import Path
import yaml

from ._stream_utils import NotSavingIO, PrintingStringIO, StreamWithTime
Expand Down Expand Up @@ -301,16 +301,16 @@ def apply(self, func: typing.Callable[[typing.Dict], typing.Optional[typing.Dict
the file system.
"""
old_db = self._db
original_path = old_db.path
original_path = Path(old_db.path)

timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M")
i = 0
new_path = f"{original_path}{timestamp}-{i}"
new_path = original_path.parent/f"{timestamp}-{i}"
while os.path.exists(new_path):
i += 1
new_path = f"{original_path}{timestamp}-{i}"
new_path = original_path.parent/f"{timestamp}-{i}"

old_db.move_database(new_path)
old_db.move_database(str(new_path))
self._db = BenchmarkDb(original_path)

for entry in old_db:
Expand Down
37 changes: 37 additions & 0 deletions tests/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,40 @@ def func(entry: typing.Dict):
assert db_count_entries(benchmark) == 10

benchmark.delete()

def test_apply_2():
benchmark = Benchmark("./test_apply_2/")

def generate_entry(args):
return {"entry_index": args["index"], "data": f"Data for entry {args['index']}"}

for i in range(20):
benchmark.add(generate_entry, {"index": i})

benchmark.compress()

def db_count_entries(b: Benchmark) -> int:
return len([0 for _ in b])

assert db_count_entries(benchmark) == 20

def func(entry: typing.Dict):
if entry["result"]["entry_index"] % 2 == 1:
return None
else:
del entry["timestamp"]
del entry["runtime"]
return entry

benchmark.apply(func)

assert db_count_entries(benchmark) == 10

for entry in benchmark:
assert "timestamp" not in entry
assert "result" in entry

benchmark.repair() # implies both a delete_if() and apply() call
assert db_count_entries(benchmark) == 10

benchmark.delete()

0 comments on commit ad038c3

Please sign in to comment.