-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
44 additions
and
20 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from copy import deepcopy | ||
|
||
|
||
# https://stackoverflow.com/a/24621200/4732868 | ||
def deepcopy_with_sharing(obj, shared_attribute_names, memo=None): | ||
""" | ||
Deepcopy an object, except for a given list of attributes, which should | ||
be shared between the original object and its copy. | ||
obj is some object | ||
shared_attribute_names: A list of strings identifying the attributes that | ||
should be shared between the original and its copy. | ||
memo is the dictionary passed into __deepcopy__. Ignore this argument if | ||
not calling from within __deepcopy__. | ||
""" | ||
assert isinstance(shared_attribute_names, (list, tuple)) | ||
shared_attributes = {k: getattr(obj, k) for k in shared_attribute_names} | ||
|
||
if hasattr(obj, "__deepcopy__"): | ||
# Do hack to prevent infinite recursion in call to deepcopy | ||
deepcopy_method = obj.__deepcopy__ | ||
obj.__deepcopy__ = None | ||
|
||
for attr in shared_attribute_names: | ||
del obj.__dict__[attr] | ||
|
||
clone = deepcopy(obj) | ||
|
||
for attr, val in shared_attributes.items(): | ||
setattr(obj, attr, val) | ||
setattr(clone, attr, val) | ||
|
||
if hasattr(obj, "__deepcopy__"): | ||
# Undo hack | ||
obj.__deepcopy__ = deepcopy_method | ||
del clone.__deepcopy__ | ||
|
||
return clone |
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