Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add more complete format string implementation for argstrings #754

Merged
merged 5 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 2 additions & 5 deletions pydra/engine/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,12 +649,9 @@ def argstr_formatting(argstr, inputs, value_updates=None):
if value_updates:
inputs_dict.update(value_updates)
# getting all fields that should be formatted, i.e. {field_name}, ...
inp_fields = re.findall(r"{\w+}", argstr)
inp_fields_float = re.findall(r"{\w+:[0-9.]+f}", argstr)
inp_fields += [re.sub(":[0-9.]+f", "", el) for el in inp_fields_float]
inp_fields = re.findall(r"{(\w+)(?::[0-9.]+f|\[[\w]+\])?}", argstr)
tclose marked this conversation as resolved.
Show resolved Hide resolved
val_dict = {}
for fld in inp_fields:
fld_name = fld[1:-1] # extracting the name form {field_name}
for fld_name in inp_fields:
fld_value = inputs_dict[fld_name]
fld_attr = getattr(attrs.fields(type(inputs)), fld_name)
if fld_value is attr.NOTHING or (
Expand Down
21 changes: 20 additions & 1 deletion pydra/engine/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import random
import platform
import pytest
import attrs
import cloudpickle as cp
from unittest.mock import Mock
from fileformats.generic import Directory, File
Expand All @@ -15,9 +16,9 @@
load_and_run,
position_sort,
parse_copyfile,
argstr_formatting,
)
from ...utils.hash import hash_function
from .. import helpers_file
from ..core import Workflow


Expand Down Expand Up @@ -311,3 +312,21 @@ def mock_field(copyfile):
parse_copyfile(mock_field((1, 2)))
with pytest.raises(TypeError, match="Unrecognised type for collation copyfile"):
parse_copyfile(mock_field((Mode.copy, 2)))


def test_argstr_formatting():
@attrs.define
class Inputs:
a1_field: str
b2_field: float
c3_field: dict[str, str]
d4_field: list[str]
tclose marked this conversation as resolved.
Show resolved Hide resolved

inputs = Inputs("1", 2.0, {"c": "3"}, ["4"])
assert (
argstr_formatting(
"{a1_field} {b2_field:02f} -test {c3_field[c]} -me {d4_field[0]}",
effigies marked this conversation as resolved.
Show resolved Hide resolved
inputs,
)
== "1 2.000000 -test 3 -me 4"
)
Loading