From c3d74ab6911d910efc6ebbd45cccfc1df5da15f1 Mon Sep 17 00:00:00 2001 From: Eirik Narjord Date: Fri, 6 Sep 2024 15:41:12 -0400 Subject: [PATCH] WIP args for all parameters in config --- src/procedures.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/procedures.py b/src/procedures.py index b825029a7..5623828b0 100644 --- a/src/procedures.py +++ b/src/procedures.py @@ -8,6 +8,8 @@ import numpy as np import pprint from copy import deepcopy +import argparse +from collections import defaultdict try: @@ -1104,6 +1106,55 @@ def fetch_market_specific_settings(config: dict): return sort_dict_keys(settings_from_exchange) +def create_acronym(full_name): + return "".join(word[0] for word in full_name.split("_")) + + +def add_arguments_recursively(parser, config, prefix=""): + acronyms = set() + + for key, value in config.items(): + full_name = f"{prefix}{key}" + + if isinstance(value, dict): + add_arguments_recursively(parser, value, f"{full_name}_") + else: + acronym = create_acronym(full_name) + i = 2 + while acronym in acronyms: + acronym = create_acronym(full_name) + str(i) + i += 1 + if i > 100: + raise Exception(f"too many acronym duplicates {acronym}") + break + parser.add_argument( + f"--{full_name}", + f"-{acronym}", + type=type(value), + dest=full_name, + required=False, + default=None, + help=f"Override {full_name}", + ) + acronyms.add(acronym) + + +def recursive_config_update(config, key, value): + if key in config: + print(f"changed {key} {config[key]} -> {value}") + config[key] = value + return True + key_split = key.split("_") + if key_split[0] in config: + return recursive_config_update(config[key_split[0]], "_".join(key_split[1:]), value) + + +def update_config_with_args(config, args): + for key, value in vars(args).items(): + if value is not None: + recursive_config_update(config, key, value) + + def main(): mssm = fetch_market_specific_settings_multi(exchange="bybit") # pprint.pprint(mssm)