Skip to content

Commit

Permalink
Merge pull request #383
Browse files Browse the repository at this point in the history
Corrects edge case in zero filling modules folder names
  • Loading branch information
joaomcteixeira committed Apr 7, 2022
2 parents 55e41bd + e9ead03 commit ed55a99
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/haddock/libs/libutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,21 @@ def get_number_of_digits(num):
return max(ceil(log10(num + 1)), 1)


def get_zerofill_for_modules(modules):
"""
Get a the prefix zerofill for modules.
If there are 5 modules, zerofill digits is 1.
If there are 10 modules, zerofill digits is 1 because counting
starts at 0 (for topoaa).
If there are 100 modules, zerofill digits is 2 because counting
starts at 0 (for topoaa).
This function is used in combination with `zero_fill`.
"""
return get_number_of_digits(len(modules) - 1)


def sort_numbered_paths(*paths):
"""
Sort input paths to tail number.
Expand Down
6 changes: 3 additions & 3 deletions src/haddock/libs/libworkflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from haddock.gear.config_reader import get_module_name
from haddock.libs.libutil import (
convert_seconds_to_min_sec,
get_number_of_digits,
get_zerofill_for_modules,
recursive_dict_update,
zero_fill,
)
Expand Down Expand Up @@ -47,7 +47,7 @@ def __init__(self, modules_parameters, **other_params):

# Create the list of steps contained in this workflow
self.steps = []
num_of_modules = get_number_of_digits(len(modules_parameters))
mod_zerofill = get_zerofill_for_modules(modules_parameters)
_items = enumerate(modules_parameters.items())
for num_stage, (stage_name, params) in _items:
log.info(f"Reading instructions of [{stage_name}] step")
Expand All @@ -61,7 +61,7 @@ def __init__(self, modules_parameters, **other_params):
_ = Step(
get_module_name(stage_name),
order=num_stage,
digits=num_of_modules,
digits=mod_zerofill,
**params_up,
)
self.steps.append(_)
Expand Down
24 changes: 24 additions & 0 deletions tests/test_libutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
file_exists,
get_number_from_path_stem,
get_number_of_digits,
get_zerofill_for_modules,
non_negative_int,
recursive_dict_update,
sort_numbered_paths,
Expand Down Expand Up @@ -203,3 +204,26 @@ def test_transform_to_list(value, expected):
)
def test_get_number_of_digits(num, expected):
assert get_number_of_digits(num) == expected


@pytest.mark.parametrize(
"mods,expected",
[
[[f"mod{i}" for i in range(1)], 1],
[[f"mod{i}" for i in range(5)], 1],
[[f"mod{i}" for i in range(10)], 1],
[[f"mod{i}" for i in range(11)], 2],
[[f"mod{i}" for i in range(99)], 2],
[[f"mod{i}" for i in range(100)], 2],
[[f"mod{i}" for i in range(101)], 3],
[[f"mod{i}" for i in range(1000)], 3],
[[f"mod{i}" for i in range(1001)], 4],
]
)
def test_get_zerofill_for_modules(mods, expected):
"""
Test zerofill for modules.
Here "mod#" strings represent modules. The number prefix means nothing.
"""
assert get_zerofill_for_modules(mods) == expected

0 comments on commit ed55a99

Please sign in to comment.