-
Notifications
You must be signed in to change notification settings - Fork 526
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
Allow construction of CUID from another CUID #3464
Merged
+34
−19
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
926d96a
allow construction of CUID from another CUID
Robbybp 4640f64
dont need to check for cuid in find_component
Robbybp e696783
dont need to check for cuid in get_indexed_cuid
Robbybp ae4e0d1
error when CUID and context are provided
Robbybp 2357c03
black
Robbybp c23320f
address review
Robbybp 5926510
fix syntax error
Robbybp 682ebbf
function in module scope
Robbybp ddbe61f
Merge branch 'main' into cuid-from-cuid
blnicho 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 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 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 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 |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
import codecs | ||
import re | ||
import ply.lex | ||
import copy | ||
|
||
from pyomo.common.collections import ComponentMap | ||
from pyomo.common.dependencies import pickle | ||
|
@@ -76,17 +77,21 @@ class ComponentUID(object): | |
def __init__(self, component, cuid_buffer=None, context=None): | ||
# A CUID can be initialized from either a reference component or | ||
# the string representation. | ||
def _context_err(_type): | ||
raise ValueError( | ||
f"Context is not allowed when initializing a ComponentUID from {_type}." | ||
) | ||
if isinstance(component, str): | ||
if context is not None: | ||
raise ValueError( | ||
"Context is not allowed when initializing a " | ||
"ComponentUID object from a string type" | ||
) | ||
_context_err(str) | ||
try: | ||
self._cids = tuple(self._parse_cuid_v2(component)) | ||
except (OSError, IOError): | ||
self._cids = tuple(self._parse_cuid_v1(component)) | ||
|
||
elif isinstance(component, ComponentUID): | ||
if context is not None: | ||
_context_err(ComponentUID) | ||
self._cids = copy.deepcopy(component._cids) | ||
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. IIRC, 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. I believe that is correct, and would be in favor of the change. |
||
elif type(component) is IndexedComponent_slice: | ||
self._cids = tuple( | ||
self._generate_cuid_from_slice(component, context=context) | ||
|
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
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.
In
find_component
, we usedtype is ComponentUID
instead ofisinstance
, presumably for performance. Should we do the same here?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.
Probably, yes. (If for no other reason than consistency - a derived class would work here, but not in
find_component
)