-
Notifications
You must be signed in to change notification settings - Fork 3
Refactor interactive argument handling #585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| from asyncio.exceptions import CancelledError | ||
| from concurrent.futures import Future, TimeoutError | ||
| from typing import Any, Union | ||
|
|
||
|
|
||
| def get_future_objects_from_input(args: tuple, kwargs: dict): | ||
| """ | ||
| Check the input parameters if they contain future objects and which of these future objects are executed | ||
|
|
||
| Args: | ||
| args (tuple): function arguments | ||
| kwargs (dict): function keyword arguments | ||
|
|
||
| Returns: | ||
| list, boolean: list of future objects and boolean flag if all future objects are already done | ||
| """ | ||
| future_lst = [] | ||
|
|
||
| def find_future_in_list(lst): | ||
| for el in lst: | ||
| if isinstance(el, Future): | ||
| future_lst.append(el) | ||
| elif isinstance(el, list): | ||
| find_future_in_list(lst=el) | ||
| elif isinstance(el, dict): | ||
| find_future_in_list(lst=el.values()) | ||
|
|
||
| find_future_in_list(lst=args) | ||
| find_future_in_list(lst=kwargs.values()) | ||
| boolean_flag = len([future for future in future_lst if future.done()]) == len( | ||
| future_lst | ||
| ) | ||
| return future_lst, boolean_flag | ||
|
|
||
|
|
||
| def get_exception_lst(future_lst: list[Future]) -> list: | ||
| """ | ||
| Get list of exceptions raised by the future objects in the list of future objects | ||
|
|
||
| Args: | ||
| future_lst (list): list of future objects | ||
|
|
||
| Returns: | ||
| list: list of exceptions raised by the future objects in the list of future objects. Returns empty list if no | ||
| exception was raised. | ||
| """ | ||
| return [ | ||
| f.exception() for f in future_lst if check_exception_was_raised(future_obj=f) | ||
| ] | ||
|
|
||
|
|
||
| def check_exception_was_raised(future_obj: Future) -> bool: | ||
| """ | ||
| Check if exception was raised by future object | ||
|
|
||
| Args: | ||
| future_obj (Future): future object | ||
|
|
||
| Returns: | ||
| bool: True if exception was raised, False otherwise | ||
| """ | ||
| try: | ||
| excp = future_obj.exception(timeout=10**-10) | ||
| return excp is not None and not isinstance(excp, CancelledError) | ||
| except TimeoutError: | ||
| return False | ||
|
|
||
|
|
||
| def update_futures_in_input(args: tuple, kwargs: dict) -> tuple[tuple, dict]: | ||
| """ | ||
| Evaluate future objects in the arguments and keyword arguments by calling future.result() | ||
|
|
||
| Args: | ||
| args (tuple): function arguments | ||
| kwargs (dict): function keyword arguments | ||
|
|
||
| Returns: | ||
| tuple, dict: arguments and keyword arguments with each future object in them being evaluated | ||
| """ | ||
|
|
||
| def get_result(arg: Union[list[Future], Future]) -> Any: | ||
| if isinstance(arg, Future): | ||
| return arg.result() | ||
| elif isinstance(arg, list): | ||
| return [get_result(arg=el) for el in arg] | ||
| elif isinstance(arg, dict): | ||
| return {k: get_result(arg=v) for k, v in arg.items()} | ||
| else: | ||
| return arg | ||
|
|
||
| args = tuple([get_result(arg=arg) for arg in args]) | ||
| kwargs = {key: get_result(arg=value) for key, value in kwargs.items()} | ||
| return args, kwargs |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| from concurrent.futures import Future | ||
| import unittest | ||
|
|
||
| from executorlib.standalone.interactive.arguments import ( | ||
| check_exception_was_raised, | ||
| get_exception_lst, | ||
| get_future_objects_from_input, | ||
| update_futures_in_input, | ||
| ) | ||
|
|
||
|
|
||
| class TestSerial(unittest.TestCase): | ||
| def test_get_future_objects_from_input_with_future(self): | ||
| input_args = (1, 2, Future(), [Future()], {3: Future()}) | ||
| input_kwargs = {"a": 1, "b": [Future()], "c": {"d": Future()}, "e": Future()} | ||
| future_lst, boolean_flag = get_future_objects_from_input(args=input_args, kwargs=input_kwargs) | ||
| self.assertEqual(len(future_lst), 6) | ||
| self.assertFalse(boolean_flag) | ||
|
|
||
| def test_get_future_objects_from_input_without_future(self): | ||
| input_args = (1, 2) | ||
| input_kwargs = {"a": 1} | ||
| future_lst, boolean_flag = get_future_objects_from_input(args=input_args, kwargs=input_kwargs) | ||
| self.assertEqual(len(future_lst), 0) | ||
| self.assertTrue(boolean_flag) | ||
|
|
||
| def test_update_futures_in_input_with_future(self): | ||
| f1 = Future() | ||
| f1.set_result(1) | ||
| f2 = Future() | ||
| f2.set_result(2) | ||
| f3 = Future() | ||
| f3.set_result(3) | ||
| f4 = Future() | ||
| f4.set_result(4) | ||
| f5 = Future() | ||
| f5.set_result(5) | ||
| f6 = Future() | ||
| f6.set_result(6) | ||
| input_args = (1, 2, f1, [f2], {3: f3}) | ||
| input_kwargs = {"a": 1, "b": [f4], "c": {"d": f5}, "e": f6} | ||
| output_args, output_kwargs = update_futures_in_input(args=input_args, kwargs=input_kwargs) | ||
| self.assertEqual(output_args, (1, 2, 1, [2], {3: 3})) | ||
| self.assertEqual(output_kwargs, {"a": 1, "b": [4], "c": {"d": 5}, "e": 6}) | ||
|
|
||
| def test_update_futures_in_input_without_future(self): | ||
| input_args = (1, 2) | ||
| input_kwargs = {"a": 1} | ||
| output_args, output_kwargs = update_futures_in_input(args=input_args, kwargs=input_kwargs) | ||
| self.assertEqual(input_args, output_args) | ||
| self.assertEqual(input_kwargs, output_kwargs) | ||
|
|
||
| def test_check_exception_was_raised(self): | ||
| f_with_exception = Future() | ||
| f_with_exception.set_exception(ValueError()) | ||
| f_without_exception = Future() | ||
| self.assertTrue(check_exception_was_raised(future_obj=f_with_exception)) | ||
| self.assertFalse(check_exception_was_raised(future_obj=f_without_exception)) | ||
|
|
||
| def test_get_exception_lst(self): | ||
| f_with_exception = Future() | ||
| f_with_exception.set_exception(ValueError()) | ||
| f_without_exception = Future() | ||
| future_with_exception_lst = [f_with_exception, f_with_exception, f_without_exception, f_without_exception, f_with_exception] | ||
| future_without_exception_lst = [f_without_exception, f_without_exception, f_without_exception, f_without_exception] | ||
| exception_lst = get_exception_lst(future_lst=future_with_exception_lst) | ||
| self.assertEqual(len(exception_lst), 3) | ||
| exception_lst = get_exception_lst(future_lst=future_without_exception_lst) | ||
| self.assertEqual(len(exception_lst), 0) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add error handling for get_future_objects_from_input.
The function call could fail if args/kwargs contain invalid types. Consider adding try-except block.
📝 Committable suggestion