Skip to content

Commit ad038c3

Browse files
committed
FIX: Apply doesn't work when path ends with /
1 parent b7fc43c commit ad038c3

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ Finally, add ``.gitattributes`` to git via
628628
Version History
629629
===============
630630

631+
- **2.4.1** Fixes bug when path ends with `/`.
631632
- **2.4.0** Removing information on installed packages due to deprecated ``pkg_resources``. New apply-function.
632633
- **2.2.2** Fixing problem with Jupyter notebooks, because they may not have a ``__file__`` attribute.
633634
- **2.2.1** Should be able to deal with corrupt zip files now.

src/algbench/benchmark.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import typing
88
import os
99
from contextlib import ExitStack, redirect_stderr, redirect_stdout
10-
10+
from pathlib import Path
1111
import yaml
1212

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

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

313-
old_db.move_database(new_path)
313+
old_db.move_database(str(new_path))
314314
self._db = BenchmarkDb(original_path)
315315

316316
for entry in old_db:

tests/test_apply.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,40 @@ def func(entry: typing.Dict):
3939
assert db_count_entries(benchmark) == 10
4040

4141
benchmark.delete()
42+
43+
def test_apply_2():
44+
benchmark = Benchmark("./test_apply_2/")
45+
46+
def generate_entry(args):
47+
return {"entry_index": args["index"], "data": f"Data for entry {args['index']}"}
48+
49+
for i in range(20):
50+
benchmark.add(generate_entry, {"index": i})
51+
52+
benchmark.compress()
53+
54+
def db_count_entries(b: Benchmark) -> int:
55+
return len([0 for _ in b])
56+
57+
assert db_count_entries(benchmark) == 20
58+
59+
def func(entry: typing.Dict):
60+
if entry["result"]["entry_index"] % 2 == 1:
61+
return None
62+
else:
63+
del entry["timestamp"]
64+
del entry["runtime"]
65+
return entry
66+
67+
benchmark.apply(func)
68+
69+
assert db_count_entries(benchmark) == 10
70+
71+
for entry in benchmark:
72+
assert "timestamp" not in entry
73+
assert "result" in entry
74+
75+
benchmark.repair() # implies both a delete_if() and apply() call
76+
assert db_count_entries(benchmark) == 10
77+
78+
benchmark.delete()

0 commit comments

Comments
 (0)