From b5af2d0a88a84b3ce86ecc6c30ce3ce2cf6a48b0 Mon Sep 17 00:00:00 2001 From: Andre Merzky Date: Sat, 6 Apr 2024 08:20:42 +0200 Subject: [PATCH] typed dict classes should have from_dict as default arg --- src/radical/utils/config.py | 15 +++++++++++---- src/radical/utils/typeddict.py | 22 ++++++++-------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/radical/utils/config.py b/src/radical/utils/config.py index f795f540..37c11f12 100644 --- a/src/radical/utils/config.py +++ b/src/radical/utils/config.py @@ -176,22 +176,23 @@ class Config(TypedDict): # -------------------------------------------------------------------------- # - def __init__(self, module=None, category=None, name=None, cfg=None, - from_dict=None, path=None, expand=True, env=None, - _internal=False): + def __init__(self, from_dict=None, + module=None, category=None, name=None, + cfg=None, path=None, expand=True, + env=None, _internal=False): """ Load a config (json) file from the module's config tree, and overload any user specific config settings if found. Parameters ---------- + from_dict: alias for cfg, to satisfy base class constructor module: used to determine the module's config file location - default: `radical.utils` category: name of config to be loaded from module's config path name: specify a specific configuration to be used path: path to app config json to be used for initialization cfg: application config dict to be used for initialization - from_dict: alias for cfg, to satisfy base class constructor expand: enable / disable environment var expansion - default: True env: environment dictionary to be used for expansion @@ -215,6 +216,12 @@ def __init__(self, module=None, category=None, name=None, cfg=None, configuration hierarchy. """ + # if the `from_dict` is given but is a string, we interpret it as + # `module` parameter. + if from_dict and isinstance(from_dict, str): + module = from_dict + from_dict = None + if from_dict: # if we could only overload constructors by signature... :-/ # As it is, we have to emulate that... diff --git a/src/radical/utils/typeddict.py b/src/radical/utils/typeddict.py index 2173f140..5d606827 100644 --- a/src/radical/utils/typeddict.py +++ b/src/radical/utils/typeddict.py @@ -488,27 +488,21 @@ def _query(self, key, default=None, last_key=True): # ------------------------------------------------------------------------------ # -def _as_dict_value(v, _annotate=False): - if isinstance(v, TypedDict): - ret = copy.deepcopy(v) - if _annotate: - ret['_type'] = type(v).__name__ - return ret - else: - return as_dict(v, _annotate) - - def as_dict(src, _annotate=False): ''' Iterate given object, apply `as_dict()` to all typed values, and return the result (effectively a shallow copy). ''' - if isinstance(src, dict): - tgt = {k: _as_dict_value(v, _annotate) for k, v in src.items()} + if isinstance(src, TypedDict): + tgt = {k: as_dict(v, _annotate) for k, v in src.items()} if _annotate: tgt['_type'] = type(src).__name__ - elif isinstance(src, collections.abc.Iterable): - tgt = [_as_dict_value(x, _annotate) for x in src] + elif isinstance(src, dict): + tgt = {k: as_dict(v, _annotate) for k, v in src.items()} + elif isinstance(src, list): + tgt = [as_dict(x, _annotate) for x in src] + elif isinstance(src, tuple): + tgt = tuple([as_dict(x, _annotate) for x in src]) else: tgt = src return tgt