Skip to content

Add to_link method to Base Entities#154

Merged
kroenlein merged 3 commits intomainfrom
feature/to-link
Oct 20, 2021
Merged

Add to_link method to Base Entities#154
kroenlein merged 3 commits intomainfrom
feature/to-link

Conversation

@kroenlein
Copy link
Copy Markdown
Collaborator

This PR adds a to_link method to Base Entities and propagates some benefits from that around the library.

to_link generates a Link By UID from the uids present in a Base Entity. If there are no uids, it raises a ValueError. If no scope to specified, it randomly selects one. If the chosen scope is not present, depending on allow_fallback it will either raise a ValueError or return a Link By UID with random scope.

One might want to add an autopopulate flag as well, but there was no immediate need for it and not including such functionality is more consistent with the library in general.

substitute_links in gemd.util was modified to use the new method. The native_uid argument for substitute_links has been deprecated, in favor of the more library-standard scope. It also has had the allow_fallback argument added, with a default that maintains the existing function behavior.

__eq__ in gemd.entity.link_by_uid was modified so that a (scope, id) tuple is equal to a similar Link By UID and make_index in gemd.util was modified to take advantage of this behavior.

@kroenlein kroenlein requested a review from bfolie October 19, 2021 23:50
Comment thread gemd/entity/base_entity.py Outdated

from gemd.entity.dict_serializable import DictSerializable
from gemd.entity.case_insensitive_dict import CaseInsensitiveDict
from gemd.entity.link_by_uid import LinkByUID
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unfortunately opens the door for circular dependencies, but I had a pickle of a time getting -> 'LinkByUID' to pass flake8.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can have flake8 ignore the line. We shouldn't let flake8 prevent us from doing the right thing (and circular dependencies in python have a way of biting you unexpectedly, in my experience).

Comment thread gemd/entity/base_entity.py Outdated
if len(self.uids) == 0:
raise ValueError(f"{type(self)} {self.name} does not have any uids.")

if scope is None or allow_fallback and scope not in self.uids:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using parentheses to indicate how these logical units are grouped, since (A | B) & C is not the same as A | (B & C).

Comment thread gemd/util/impl.py
"""
def _make_index(_obj: BaseEntity):
return (((scope, _obj.uids[scope]), _obj) for scope in _obj.uids)
return ((LinkByUID(scope=scope, id=_obj.uids[scope]), _obj) for scope in _obj.uids)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have any consequences besides the modification to _substitute?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently DE has built tooling that uses the make_index & tuple representation. With the eq changes to LinkByUID, we should have a drop-in replacement -- I've reached out to DE to verify. I've verified with the citrine-python test suite.

The alternative would be to remove the tuple/LinkByUID equality and have both types of objects serve as indices.

Specially formatted tuples are generally considered bad practice to my understanding.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified that the DE tooling that uses make_index continues to pass tests with these changes.

Copy link
Copy Markdown

@bfolie bfolie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can this not be done with the LinkByUID.from_entity method?

@kroenlein
Copy link
Copy Markdown
Collaborator Author

Why can this not be done with the LinkByUID.from_entity method?

They have similar behavior, yes. The only difference at present is from_entity will generate a new id -- thus the comment regarding autopopulate. This also gives us an opportunity to deprecate methods that could surprise users with object mutations.

However:

  • from a coding perspective it's annoying to constantly import an additional core package and write a long method sig for a simple concept that's used ubiquitously
  • using LinkByUID as a factory class is an odd choice to generate something that doesn't really require external logic or information

@kroenlein kroenlein merged commit 1b648c3 into main Oct 20, 2021
@kroenlein kroenlein deleted the feature/to-link branch October 20, 2021 21:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants