Skip to content

Commit

Permalink
Add multiblock copy method and negative indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
banesullivan committed May 3, 2019
1 parent 423eb1b commit 1481696
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tests/test_container.py
Expand Up @@ -214,3 +214,39 @@ def test_combine_filter():
# Now apply the geometry filter to combine a plethora of data blocks
geom = multi.combine()
assert isinstance(geom, vtki.UnstructuredGrid)



def test_multi_block_copy():
multi = vtki.MultiBlock()
# Add examples
multi.append(ex.load_ant())
multi.append(ex.load_sphere())
multi.append(ex.load_uniform())
multi.append(ex.load_airplane())
multi.append(ex.load_globe())
# Now check everything
newobj = multi.copy()
assert multi.n_blocks == 5 == newobj.n_blocks
assert id(multi[0]) != id(newobj[0])
assert id(multi[-1]) != id(newobj[-1])
return


def test_multi_block_negative_index():
multi = vtki.MultiBlock()
# Add examples
multi.append(ex.load_ant())
multi.append(ex.load_sphere())
multi.append(ex.load_uniform())
multi.append(ex.load_airplane())
multi.append(ex.load_globe())
# Now check everything
assert id(multi[-1]) == id(multi[4])
assert id(multi[-2]) == id(multi[3])
assert id(multi[-3]) == id(multi[2])
assert id(multi[-4]) == id(multi[1])
assert id(multi[-5]) == id(multi[0])
with pytest.raises(IndexError):
foo = multi[-6]
return
36 changes: 36 additions & 0 deletions vtki/container.py
Expand Up @@ -221,6 +221,10 @@ def __getitem__(self, index):
returns the first occurence)"""
if isinstance(index, str):
index = self.get_index_by_name(index)
if index < 0:
index = self.n_blocks + index
if index < 0 or index >= self.n_blocks:
raise IndexError('index ({}) out of range for this dataset.'.format(index))
data = self.GetBlock(index)
if data is None:
return data
Expand Down Expand Up @@ -415,3 +419,35 @@ def __repr__(self):

def __str__(self):
return MultiBlock.__repr__(self)


def copy_meta_from(self, ido):
"""Copies vtki meta data onto this object from another object"""
# Note that `vtki.MultiBlock` datasets currently don't have any meta.
# This method is here for consistency witht the rest of the API and
# incase we add meta data to this pbject down the road.
pass


def copy(self, deep=True):
"""
Returns a copy of the object
Parameters
----------
deep : bool, optional
When True makes a full copy of the object.
Returns
-------
newobject : same as input
Deep or shallow copy of the input.
"""
thistype = type(self)
newobject = thistype()
if deep:
newobject.DeepCopy(self)
else:
newobject.ShallowCopy(self)
newobject.copy_meta_from(self)
return newobject

0 comments on commit 1481696

Please sign in to comment.