-
Notifications
You must be signed in to change notification settings - Fork 4
fix: ensure that format for times can be accessed and supplied in duc… #135
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
4 commits
Select commit
Hold shift + click to select a range
3b96439
fix: ensure that format for times can be accessed and supplied in duc…
stevenhsd 9e51d1a
style: resolve formatting and linting issues
stevenhsd 934f9de
docs: tweak docstring for resilient_get
stevenhsd eef3e2c
Merge branch 'release_v084' of https://github.com/NHSDigital/data-val…
stevenhsd 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -52,3 +52,23 @@ def chain_get( | |||||
| return result | ||||||
|
|
||||||
| raise exc.TypeNotFoundError(f"Callable or type ({item!r}) not found") | ||||||
|
|
||||||
|
|
||||||
| def resilient_get(item: object, *attribute_names: str) -> Any: | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed docs instead - using *args to gather into a tuple |
||||||
| """Given a number of attribute names, try to get attribute value | ||||||
| sequentially. Returns the first value found, and if no attributes found | ||||||
| returns None. | ||||||
|
|
||||||
| Args: | ||||||
| item (object): The object to obtain attributes from (where possible) | ||||||
| attribute_names (str): The attribute names to search for | ||||||
|
|
||||||
| Returns: | ||||||
| Any: The first found attribute, otherwise None | ||||||
| """ | ||||||
| for attr in attribute_names: | ||||||
| try: | ||||||
| return getattr(item, attr) | ||||||
| except AttributeError: | ||||||
| continue | ||||||
| return None | ||||||
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,24 @@ | ||
| import pytest | ||
| from dve.metadata_parser.utilities import resilient_get | ||
|
|
||
| class MyParent: | ||
| cls_attr = "hello" | ||
| def __init__(self, my_attr:str, another_attr:str): | ||
| self.my_attr = my_attr | ||
| self.another_attr = another_attr | ||
|
|
||
| class MyObject(MyParent): | ||
| sub_attr = "bye" | ||
| def __init__(self, extra_attr:int): | ||
| self.extra_attr = extra_attr | ||
| super().__init__("from", "child") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("obj,attrs,expected", [(MyParent, ("cls_attr",), "hello"), | ||
| (MyObject, ("cls_attr", "sub_attr"), "hello"), | ||
| (MyObject, ("sub_attr", "cls_attr"), "bye"), | ||
| (MyParent, ("my_attr",), None), | ||
| (MyParent("this", "test"), ("extra_attr", "my_attr"), "this"), | ||
| (MyObject("this"), ("daft_attr", "another_daft_attr", "yet_another", "another_attr"), "child")]) | ||
| def test_resilient_get(obj, attrs, expected): | ||
| assert resilient_get(obj, *attrs) == expected |
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.
Are you not checking for "TIME_FORMAT" here as well?
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.
version of spark being used doesnt support time datatype at the moment - better to not try to cast to a type that doesn't exist