Merge branch 'table-getitem' into development

This commit is contained in:
Martin Diehl 2020-12-12 19:58:42 +01:00
commit aad123f41b
2 changed files with 12 additions and 3 deletions

View File

@ -33,6 +33,10 @@ class Table:
"""Brief overview.""" """Brief overview."""
return '\n'.join(['# '+c for c in self.comments])+'\n'+self.data.__repr__() return '\n'.join(['# '+c for c in self.comments])+'\n'+self.data.__repr__()
def __getitem__(self,item):
"""Return slice according to item."""
return self.__class__(data=self.data[item],shapes=self.shapes,comments=self.comments)
def __len__(self): def __len__(self):
"""Number of rows.""" """Number of rows."""
return len(self.data) return len(self.data)

View File

@ -8,7 +8,7 @@ from damask import Table
def default(): def default():
"""Simple Table.""" """Simple Table."""
x = np.ones((5,13),dtype=float) x = np.ones((5,13),dtype=float)
return Table(x,{'F':(3,3),'v':(3,),'s':(1,)},['test data','contains only ones']) return Table(x,{'F':(3,3),'v':(3,),'s':(1,)},['test data','contains five rows of only ones'])
@pytest.fixture @pytest.fixture
def ref_path(ref_path_base): def ref_path(ref_path_base):
@ -20,8 +20,9 @@ class TestTable:
def test_repr(self,default): def test_repr(self,default):
print(default) print(default)
def test_len(self): @pytest.mark.parametrize('N',[10,40])
len(Table(np.random.rand(7,3),{'X':3})) == 7 def test_len(self,N):
len(Table(np.random.rand(N,3),{'X':3})) == N
def test_get_scalar(self,default): def test_get_scalar(self,default):
d = default.get('s') d = default.get('s')
@ -39,6 +40,10 @@ class TestTable:
d = default.get('5_F') d = default.get('5_F')
assert np.allclose(d,1.0) and d.shape[1:] == (1,) assert np.allclose(d,1.0) and d.shape[1:] == (1,)
@pytest.mark.parametrize('N',[10,40])
def test_getitem(self,N):
assert len(Table(np.random.rand(N,1),{'X':1})[:N//2]) == N//2
@pytest.mark.parametrize('mode',['str','path']) @pytest.mark.parametrize('mode',['str','path'])
def test_write_read(self,default,tmp_path,mode): def test_write_read(self,default,tmp_path,mode):
default.save(tmp_path/'default.txt') default.save(tmp_path/'default.txt')