-
Notifications
You must be signed in to change notification settings - Fork 18
Implement _collection_ref to link objects to their NumberedObjectCollection parent #867
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
base: develop
Are you sure you want to change the base?
Changes from all commits
83b61f7
ae3e6c1
622632d
3ca4d87
de5c480
7fbe1b7
c202b8b
01ffc25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -178,6 +178,7 @@ def __init__( | |
| ) | ||
| ) | ||
| self.__num_cache[obj.number] = obj | ||
| self._link_to_collection(obj) | ||
| self._objects = objects | ||
|
|
||
| def link_to_problem(self, problem): | ||
|
|
@@ -198,13 +199,38 @@ def link_to_problem(self, problem): | |
| self._problem_ref = weakref.ref(problem) | ||
| for obj in self: | ||
| obj.link_to_problem(problem) | ||
| # the _collection_ref that points to the main cells collection. | ||
| if problem is not None: | ||
| existing_coll = obj._collection | ||
| if existing_coll is None or existing_coll._problem is not problem: | ||
| self._link_to_collection(obj) | ||
|
|
||
| @property | ||
| def _problem(self): | ||
| if self._problem_ref is not None: | ||
| return self._problem_ref() | ||
| return None | ||
|
|
||
| def _link_to_collection(self, obj): | ||
|
Collaborator
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. Sorry this is the flip of what I meant. I think these should be functions of
Collaborator
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. The reason being is objects should respect each other's private attributes. I am ok with calling each other's private functions as that in my mind is more marking it as internal use only.
Collaborator
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 think this should still be removed, now that a similar function exists in |
||
| """Links the given object to this collection via a weakref. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| obj : Numbered_MCNP_Object | ||
| The object to link to this collection. | ||
| """ | ||
| obj._link_to_collection(self) | ||
|
|
||
| def _unlink_from_collection(self, obj): | ||
| """Unlinks the given object from this collection. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| obj : Numbered_MCNP_Object | ||
| The object to unlink from this collection. | ||
| """ | ||
| obj._unlink_from_collection() | ||
|
|
||
| def __getstate__(self): | ||
| state = self.__dict__.copy() | ||
| weakref_key = "_problem_ref" | ||
|
|
@@ -341,10 +367,8 @@ def extend(self, other_list): | |
| ) | ||
| if obj.number in nums: | ||
| raise NumberConflictError( | ||
| ( | ||
| f"When adding to {type(self).__name__} there was a number collision due to " | ||
| f"adding {obj} which conflicts with {self[obj.number]}" | ||
| ) | ||
| f"When adding to {type(self).__name__} there was a number collision due to " | ||
| f"adding {obj} which conflicts with existing object number {obj.number}" | ||
| ) | ||
| nums.add(obj.number) | ||
| for obj in other_list: | ||
|
|
@@ -496,6 +520,7 @@ def __internal_append(self, obj, **kwargs): | |
| ) | ||
| self.__num_cache[obj.number] = obj | ||
| self._objects.append(obj) | ||
| self._link_to_collection(obj) | ||
|
Collaborator
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. This should be |
||
| self._append_hook(obj, **kwargs) | ||
| if self._problem: | ||
| obj.link_to_problem(self._problem) | ||
|
|
@@ -507,6 +532,7 @@ def __internal_delete(self, obj, **kwargs): | |
| """ | ||
| self.__num_cache.pop(obj.number, None) | ||
| self._objects.remove(obj) | ||
| self._unlink_from_collection(obj) | ||
digvijay-y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self._delete_hook(obj, **kwargs) | ||
|
|
||
| def add(self, obj: Numbered_MCNP_Object): | ||
|
|
@@ -613,6 +639,7 @@ def append_renumber(self, obj, step=1): | |
| number = obj.number if obj.number > 0 else 1 | ||
| if self._problem: | ||
| obj.link_to_problem(self._problem) | ||
| self._unlink_from_collection(obj) | ||
|
Collaborator
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. ditto. |
||
| try: | ||
| self.append(obj) | ||
| except (NumberConflictError, ValueError) as e: | ||
|
|
||
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.
This is still not the desired behavior in all cases. Generally they should be added to the same problem. I know that this can lead to a number collision in an edge case with hypothesis, that I haven't fixed yet, and I just ignore it with
rm -r .hypothesis. I think that's a separate issue from this, and I need to open a bug report. Try it without these lines, and with purging the.hypothesisfolder.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 still don't think this should be done.