-
Notifications
You must be signed in to change notification settings - Fork 0
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
Timx 287 ead field method refactor 3 #190
Conversation
a89ed45
to
ec3b026
Compare
@@ -31,7 +31,6 @@ def get_optional_fields(self, source_record: Tag) -> dict | None: | |||
|
|||
# <archdesc> and <did> elements are required when deriving optional fields | |||
collection_description = self._get_collection_description(source_record) | |||
collection_description_did = self._get_collection_description_did(source_record) |
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.
Methods have been created for all fields that retrieved data from <archdesc><did>
, thus this line of code is not needed!
languages = [] | ||
collection_description_did = cls._get_collection_description_did(source_record) | ||
for langmaterial_element in collection_description_did.find_all( | ||
"langmaterial", recursive=False | ||
): | ||
languages.extend( | ||
[ | ||
language | ||
for language_element in langmaterial_element.find_all("language") | ||
if (language := cls.create_string_from_mixed_value(language_element)) | ||
] | ||
) |
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.
For field methods that involved nested for loops, the following pattern is applied:
- instantiate list instance (
languages = []
) - external for loop statement
- extend list instance to list comprehension of inner for loop
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.
I appreciate the comment explaing this, as I may have passed over this syntax and approach more quickly otherwise. This is interesting: I'm not sure I've seen a walrus operator as part of a list comprehension.
I cooked this up to help me unpack it:
nums = list(range(0,10))
def even_num_strings(num):
if num % 2 == 0:
return str(num)
[
even_num
for num in nums
if (even_num := even_num_strings(num))
]
# Out[7]: ['0', '2', '4', '6', '8']
I must say, I kind of like it! I think this post has a nice example where it can avoid duplicate API or otherwise expensive calls. I think it's a bit more complex than some lists and for
loops, but I'm on board.
for note_element in collection_description.find_all( | ||
[ | ||
"bibliography", | ||
"bioghist", | ||
"scopecontent", | ||
], | ||
recursive=False, | ||
): | ||
subelement_tag = "bibref" if note_element.name == "bibliography" else "p" |
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.
Just noting that XPath + lxml
may be useful in derivations like this one!
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.
As dicussed on a quick call, I agree. And agree, that given the scope of this field method refactoring, probably not the time to make, or even fully explore, the switch.
But helpful to have these concrete examples if and when we do revisit that.
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.
Looks good! Left a couple of conversational comments, but nothing blocking.
languages = [] | ||
collection_description_did = cls._get_collection_description_did(source_record) | ||
for langmaterial_element in collection_description_did.find_all( | ||
"langmaterial", recursive=False | ||
): | ||
languages.extend( | ||
[ | ||
language | ||
for language_element in langmaterial_element.find_all("language") | ||
if (language := cls.create_string_from_mixed_value(language_element)) | ||
] | ||
) |
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.
I appreciate the comment explaing this, as I may have passed over this syntax and approach more quickly otherwise. This is interesting: I'm not sure I've seen a walrus operator as part of a list comprehension.
I cooked this up to help me unpack it:
nums = list(range(0,10))
def even_num_strings(num):
if num % 2 == 0:
return str(num)
[
even_num
for num in nums
if (even_num := even_num_strings(num))
]
# Out[7]: ['0', '2', '4', '6', '8']
I must say, I kind of like it! I think this post has a nice example where it can avoid duplicate API or otherwise expensive calls. I think it's a bit more complex than some lists and for
loops, but I'm on board.
for note_element in collection_description.find_all( | ||
[ | ||
"bibliography", | ||
"bioghist", | ||
"scopecontent", | ||
], | ||
recursive=False, | ||
): | ||
subelement_tag = "bibref" if note_element.name == "bibliography" else "p" |
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.
As dicussed on a quick call, I agree. And agree, that given the scope of this field method refactoring, probably not the time to make, or even fully explore, the switch.
But helpful to have these concrete examples if and when we do revisit that.
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.
Great work, just a few suggestions!
transmogrifier/sources/xml/ead.py
Outdated
@classmethod | ||
def get_locations(cls, source_record: Tag) -> list[timdex.Location] | None: | ||
locations = [] | ||
control_access_elements = cls._get_control_access(source_record) |
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.
Move cls._get_control_access(source_record)
to for
loop?
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.
Moved. See cc25014. I moved calls to private methods when they weren't followed by a .find
/.findall
.
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.
Fantastic work on this transform!
Why these changes are being introduced: * These updates are required to implement the architecture described in the following ADR: https://github.com/MITLibraries/transmogrifier/blob/main/docs/adrs/0005-field-methods.md How this addresses that need: * Add field methods and corresponding unit tests: languages, locations, notes, physical description, publishers, and summary Side effects of this change: * None Relevant ticket(s): * https://mitlibraries.atlassian.net/browse/TIMX-287
cc25014
to
c30a779
Compare
Purpose and background context
Field method refactor for transform class Ead (Part 3).
languages
,locations
,notes
,physical description
,publishers
, andsummary
How can a reviewer manually see the effects of these changes?
Run
make test
and verify all unit tests are passing.Run CLI command
Output:
Includes new or updated dependencies?
NO
Changes expectations for external applications?
NO
What are the relevant tickets?
Developer
Code Reviewer(s)