Last active
May 23, 2018 14:24
-
-
Save chobeat/7cdfd919d8be842be49607cf24195038 to your computer and use it in GitHub Desktop.
Naive functional error collection in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def execute_with_error_collection(execution_spec, exception, args=None, kwargs=None): | |
""" | |
Allows to functionally collect errors in a given set of operations by specifying lazily the operations to execute | |
with the relative arguments | |
:param execution_spec: lazy specification of the operations to execute. Expected fields: function, args, kwargs. | |
:param exception: the type of exception to collect. Use Exception to capture everything. | |
:param args: overrides the args field in the spec if specified. Used to apply all the functions on the same argument | |
set without repetition in the spec. | |
:param kwargs: overrides the kwargs field in the spec if specified. Used to apply all the functions on the same | |
argument set without repetition in the spec. | |
:return: a tuple of results and errors | |
""" | |
errors = {} | |
results = {} | |
for key, function_with_args in execution_spec.items(): | |
function = function_with_args["function"] | |
args = function_with_args.setdefault("args", []) if not args else args | |
kwargs = function_with_args.setdefault("kwargs", {}) if not kwargs else kwargs | |
try: | |
results[key] = function(*args, **kwargs) | |
except exception as e: | |
errors[key] = e | |
return results, errors |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment