diff --git a/aea/helpers/env_vars.py b/aea/helpers/env_vars.py index da067c76eb..326d2bab46 100644 --- a/aea/helpers/env_vars.py +++ b/aea/helpers/env_vars.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # ------------------------------------------------------------------------------ # -# Copyright 2022 Valory AG +# Copyright 2022-2023 Valory AG # Copyright 2018-2019 Fetch.AI Limited # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -50,6 +50,29 @@ def export_path_to_env_var_string(export_path: List[str]) -> str: NotSet = object() +def parse_list(var_prefix: str, env_variables: dict) -> str: + """Parse list object.""" + values = {} + _vars = list(filter(lambda x: x.startswith(var_prefix), env_variables.keys())) + for var in _vars: + _, idx, *sub_var = var.replace(var_prefix, "").split("_") + if len(sub_var) > 0: + values[idx] = json.loads( + parse_list( + var_prefix=f"{var_prefix}_{idx}", + env_variables=env_variables, + ) + ) + continue + try: + values[idx] = json.loads(str(env_variables[var])) + except (json.JSONDecodeError, ValueError): + values[idx] = env_variables[var] + if all(map(lambda x: isinstance(json.loads(x), int), values.keys())): + return json.dumps([values[idx] for idx in sorted(values)]) + return json.dumps({json.loads(key): val for key, val in values.items()}) + + def replace_with_env_var( value: str, env_variables: dict, @@ -68,6 +91,11 @@ def replace_with_env_var( if var_name in env_variables: var_value = env_variables[var_name] + elif type_str == "list": + var_value = parse_list( + var_prefix=var_name, + env_variables=env_variables, + ) elif default is not None: var_value = default elif default_value is not NotSet: @@ -233,7 +261,7 @@ def generate_env_vars_recursively( if is_strict_list(data=data): env_var_dict[ export_path_to_env_var_string(export_path=export_path) - ] = json.dumps(data) + ] = json.dumps(data, separators=(",", ":")) else: for key, value in enumerate(data): env_var_dict.update( diff --git a/docs/api/helpers/env_vars.md b/docs/api/helpers/env_vars.md index dc6719f7b9..d34e6adb8e 100644 --- a/docs/api/helpers/env_vars.md +++ b/docs/api/helpers/env_vars.md @@ -24,6 +24,16 @@ def export_path_to_env_var_string(export_path: List[str]) -> str Conver export path to environment variable string. + + +#### parse`_`list + +```python +def parse_list(var_prefix: str, env_variables: dict) -> str +``` + +Parse list object. + #### replace`_`with`_`env`_`var diff --git a/tests/test_helpers/test_env_vars.py b/tests/test_helpers/test_env_vars.py index cdd5369f27..4a9350b8e2 100644 --- a/tests/test_helpers/test_env_vars.py +++ b/tests/test_helpers/test_env_vars.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # ------------------------------------------------------------------------------ # -# Copyright 2022 Valory AG +# Copyright 2022-2023 Valory AG # Copyright 2018-2019 Fetch.AI Limited # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -184,6 +184,33 @@ def test_env_var_string_generator(export_path: List[str], var_string: str) -> No ] }, ), + ( + { + "stratagies_kwargs": [ + ["bet_kelly_fraction", 0.25], + ["floor_balance", 500000000000000000], + [ + "bet_amount_per_threshold", + { + "0.0": 0, + "0.1": 0, + "0.2": 0, + "0.3": 0, + "0.4": 0, + "0.5": 0, + "0.6": 60000000000000000, + "0.7": 90000000000000000, + "0.8": 100000000000000000, + "0.9": 1000000000000000000, + "1.0": 10000000000000000000, + }, + ], + ], + }, + { + "stratagies_kwargs": "${list:[]}", + }, + ), ], ) def test_match_export_parse_consistency(export_data, template) -> None: