Skip to content

Commit

Permalink
Merge pull request #700 from valory-xyz/fix/env-var-parsing
Browse files Browse the repository at this point in the history
Fix nested list parsing
  • Loading branch information
angrybayblade authored Dec 14, 2023
2 parents e3b608a + 326a54a commit 38c233c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
32 changes: 30 additions & 2 deletions aea/helpers/env_vars.py
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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(
Expand Down
10 changes: 10 additions & 0 deletions docs/api/helpers/env_vars.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ def export_path_to_env_var_string(export_path: List[str]) -> str

Conver export path to environment variable string.

<a id="aea.helpers.env_vars.parse_list"></a>

#### parse`_`list

```python
def parse_list(var_prefix: str, env_variables: dict) -> str
```

Parse list object.

<a id="aea.helpers.env_vars.replace_with_env_var"></a>

#### replace`_`with`_`env`_`var
Expand Down
29 changes: 28 additions & 1 deletion tests/test_helpers/test_env_vars.py
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 38c233c

Please sign in to comment.