Skip to content
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

Copy Attributes when copy is used. #437

Merged
merged 1 commit into from
Nov 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions nbodykit/base/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,10 @@ def copy(self):
.. note::
No copy of data is made.

.. note::
This is different from view in that the attributes dictionary
of the copy no longer related to ``self``.

Returns
-------
CatalogSource :
Expand All @@ -488,6 +492,10 @@ def copy(self):
for col in self.columns:
toret[col] = self[col]

# copy the attributes too, so they become decoupled
# this is different from view.
toret._attrs = self._attrs.copy()

return toret

def get_hardcolumn(self, col):
Expand Down
12 changes: 12 additions & 0 deletions nbodykit/base/tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,14 @@ def test_copy(comm):
for k in source.attrs:
assert k in copy.attrs

# adding columns to the copy doesn't add to original source
copy['TEST2'] = 5.0
assert 'TEST2' not in source

# make sure attrs are independent.
source.attrs['foo'] = 123
assert 'foo' not in copy.attrs

@MPITest([4])
def test_view(comm):
CurrentMPIComm.set(comm)
Expand All @@ -309,3 +317,7 @@ def test_view(comm):
# adding columns to the view changes original source
view['TEST2'] = 5.0
assert 'TEST2' in source

# make sure attrs are dependent.
source.attrs['foo'] = 123
assert 'foo' in view.attrs