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

feat(framework) Allow multiple separated run-config arguments #3824

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions src/py/flwr/cli/run/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import sys
from logging import DEBUG
from pathlib import Path
from typing import Any, Dict, Optional
from typing import Any, Dict, List, Optional

import typer
from typing_extensions import Annotated
Expand All @@ -43,7 +43,7 @@ def run(
typer.Argument(help="Name of the federation to run the app on"),
] = None,
config_overrides: Annotated[
Optional[str],
Optional[List[str]],
typer.Option(
"--run-config",
"-c",
Expand Down Expand Up @@ -113,7 +113,7 @@ def run(
def _run_with_superexec(
federation: Dict[str, str],
directory: Optional[Path],
config_overrides: Optional[str],
config_overrides: Optional[List[str]],
) -> None:

def on_channel_state_change(channel_connectivity: str) -> None:
Expand Down Expand Up @@ -172,7 +172,7 @@ def _run_without_superexec(
app_path: Optional[Path],
federation: Dict[str, Any],
federation_name: str,
config_overrides: Optional[str],
config_overrides: Optional[List[str]],
) -> None:
try:
num_supernodes = federation["options"]["num-supernodes"]
Expand Down
27 changes: 14 additions & 13 deletions src/py/flwr/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def flatten_dict(raw_dict: Dict[str, Any], parent_key: str = "") -> Dict[str, st


def parse_config_args(
config: Optional[str],
config: Optional[List[str]],
separator: str = ",",
) -> Dict[str, str]:
"""Parse separator separated list of key-value pairs separated by '='."""
Expand All @@ -139,17 +139,18 @@ def parse_config_args(
if config is None:
return overrides

overrides_list = config.split(separator)
if (
len(overrides_list) == 1
and "=" not in overrides_list
and overrides_list[0].endswith(".toml")
):
with Path(overrides_list[0]).open("rb") as config_file:
overrides = flatten_dict(tomli.load(config_file))
else:
for kv_pair in overrides_list:
key, value = kv_pair.split("=")
overrides[key] = value
for config_line in config:
overrides_list = config_line.split(separator)
if (
len(overrides_list) == 1
and "=" not in overrides_list
and overrides_list[0].endswith(".toml")
):
with Path(overrides_list[0]).open("rb") as config_file:
overrides = flatten_dict(tomli.load(config_file))
else:
for kv_pair in overrides_list:
key, value = kv_pair.split("=")
overrides[key] = value

return overrides
7 changes: 6 additions & 1 deletion src/py/flwr/common/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,12 @@ def test_parse_config_args_none() -> None:

def test_parse_config_args_overrides() -> None:
"""Test parse_config_args with key-value pairs."""
assert parse_config_args("key1=value1,key2=value2") == {
assert parse_config_args(
["key1=value1,key2=value2", "key3=value3", "key4=value4,key5=value5"]
) == {
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4",
"key5": "value5",
}