from argparse import Namespace def strtobool(val): """Convert a string representation of truth to true (1) or false (0). Copied from the python implementation distutils.utils.strtobool True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if 'val' is anything else. >>> strtobool('YES') 1 >>> strtobool('FALSE') 0 """ val = val.lower() if val in ('y', 'yes', 't', 'true', 'on', '1'): return 1 elif val in ('n', 'no', 'f', 'false', 'off', '0'): return 0 else: raise ValueError(f'invalid truth value {val}') def clean_namespace(hparams): """ Removes all functions from hparams so we can pickle :param hparams: :return: """ if isinstance(hparams, Namespace): del_attrs = [] for k in hparams.__dict__: if callable(getattr(hparams, k)): del_attrs.append(k) for k in del_attrs: delattr(hparams, k) elif isinstance(hparams, dict): del_attrs = [] for k, v in hparams.items(): if callable(v): del_attrs.append(k) for k in del_attrs: del hparams[k]