Run method method_obj with *argv as arguments. Create dictionary of all input/ output and other meta data elements to be eventually logged to file. :param method_obj: method reference :param del_self_arg: True if we shouldn't include `self` variable in output dictionary :param
(method_obj, del_self_arg: bool, *argv, **kwargs)
| 8 | |
| 9 | |
| 10 | def run_method_get_io_dict(method_obj, del_self_arg: bool, *argv, **kwargs) -> Dict: |
| 11 | """ |
| 12 | Run method method_obj with *argv as arguments. |
| 13 | Create dictionary of all input/ output and other meta data elements to be eventually logged to file. |
| 14 | |
| 15 | :param method_obj: method reference |
| 16 | :param del_self_arg: True if we shouldn't include `self` variable in output dictionary |
| 17 | :param argv: Arguments that needs to be passed to method as *argv |
| 18 | :param kwargs: Arguments that needs to be passed to method as **kwargs |
| 19 | |
| 20 | :return: Dict that has inputs, outputs and meta data to be logged |
| 21 | """ |
| 22 | args_to_log = defaultdict(dict) |
| 23 | |
| 24 | start_time = time() |
| 25 | output = method_obj(*argv, **kwargs) |
| 26 | execution_time = time() - start_time |
| 27 | |
| 28 | # get name of input parameters of method method_obj |
| 29 | arg_spec = getfullargspec(method_obj) |
| 30 | arg_names = arg_spec.args |
| 31 | argv_list = list(argv) |
| 32 | |
| 33 | # Capture all *argv values |
| 34 | for arg_name, arg_val in zip(arg_names[:len(argv_list)], argv_list): |
| 35 | if isinstance(arg_val, Hashable) and not (del_self_arg and arg_name == "self"): |
| 36 | args_to_log[LogLiterals.INPUTS][arg_name] = str(arg_val) |
| 37 | |
| 38 | # Capture all **kwargs values |
| 39 | args_to_log[LogLiterals.INPUTS].update(kwargs) |
| 40 | |
| 41 | if arg_spec.defaults: |
| 42 | default_arg_values = list(arg_spec.defaults) |
| 43 | # For args that don't have any value, set defaults |
| 44 | arg_with_no_values_count = len(arg_names) - (len(argv_list) + len(kwargs)) |
| 45 | # Number of arguments for which defaults should be used |
| 46 | defaults_count = min(arg_with_no_values_count, len(default_arg_values)) |
| 47 | |
| 48 | # Arguments for which values are not passed but defaults are specified, use defaults |
| 49 | for arg_names, arg_val in zip(arg_names[-defaults_count:], default_arg_values[-defaults_count:]): |
| 50 | if isinstance(arg_val, Hashable): |
| 51 | args_to_log[LogLiterals.INPUTS][arg_name] = str(arg_val) |
| 52 | |
| 53 | args_to_log[LogLiterals.OUTPUTS] = output |
| 54 | args_to_log[LogLiterals.META][LogLiterals.EXEC_SEC] = execution_time |
| 55 | args_to_log[LogLiterals.META][LogLiterals.TIMESTAMP] = datetime.now() |
| 56 | |
| 57 | return args_to_log |