Update the `args` dictionary with the input `kwargs`. For dict data, recursively update the content based on the keys. Example:: from monai.bundle import update_kwargs update_kwargs({'exist': 1}, exist=2, new_arg=3) # return {'exist': 2, 'new_arg': 3} Args
(args: str | dict | None = None, ignore_none: bool = True, **kwargs: Any)
| 72 | |
| 73 | |
| 74 | def update_kwargs(args: str | dict | None = None, ignore_none: bool = True, **kwargs: Any) -> dict: |
| 75 | """ |
| 76 | Update the `args` dictionary with the input `kwargs`. |
| 77 | For dict data, recursively update the content based on the keys. |
| 78 | |
| 79 | Example:: |
| 80 | |
| 81 | from monai.bundle import update_kwargs |
| 82 | update_kwargs({'exist': 1}, exist=2, new_arg=3) |
| 83 | # return {'exist': 2, 'new_arg': 3} |
| 84 | |
| 85 | Args: |
| 86 | args: source `args` dictionary (or a json/yaml filename to read as dictionary) to update. |
| 87 | ignore_none: whether to ignore input args with None value, default to `True`. |
| 88 | kwargs: key=value pairs to be merged into `args`. |
| 89 | |
| 90 | """ |
| 91 | args_: dict = args if isinstance(args, dict) else {} |
| 92 | if isinstance(args, str): |
| 93 | # args are defined in a structured file |
| 94 | args_ = ConfigParser.load_config_file(args) |
| 95 | if isinstance(args, (tuple, list)) and all(isinstance(x, str) for x in args): |
| 96 | primary, overrides = args |
| 97 | args_ = update_kwargs(primary, ignore_none, **update_kwargs(overrides, ignore_none, **kwargs)) |
| 98 | if not isinstance(args_, dict): |
| 99 | return args_ |
| 100 | # recursively update the default args with new args |
| 101 | for k, v in kwargs.items(): |
| 102 | if ignore_none and v is None: |
| 103 | continue |
| 104 | if isinstance(v, dict) and isinstance(args_.get(k), dict): |
| 105 | args_[k] = update_kwargs(args_[k], ignore_none, **v) |
| 106 | else: |
| 107 | merge_kv(args_, k, v) |
| 108 | return args_ |
| 109 | |
| 110 | |
| 111 | _update_args = update_kwargs # backward compatibility |
searching dependent graphs…