(dict_to_filter: dict, func_with_kwargs: Callable)
| 104 | # function (func_with_kwargs.) |
| 105 | # Adapted from https://stackoverflow.com/questions/26515595/how-does-one-ignore-unexpected-keyword-arguments-passed-to-a-function/44052550 |
| 106 | def filter_dict(dict_to_filter: dict, func_with_kwargs: Callable) -> dict: |
| 107 | import inspect |
| 108 | |
| 109 | filter_keys = [] |
| 110 | try: |
| 111 | # Python3 ... |
| 112 | sig = inspect.signature(func_with_kwargs) |
| 113 | filter_keys = [param.name for param in sig.parameters.values()] |
| 114 | except: |
| 115 | # Python2 ... |
| 116 | filter_keys = inspect.getargspec(func_with_kwargs)[0] |
| 117 | |
| 118 | filtered_dict = { |
| 119 | filter_key: dict_to_filter[filter_key] |
| 120 | for filter_key in filter_keys |
| 121 | if filter_key in dict_to_filter |
| 122 | } |
| 123 | return filtered_dict |
| 124 | |
| 125 | |
| 126 | # ------------------------------------------------------- # |
no outgoing calls
no test coverage detected