Skip to content

Commit

Permalink
Allow printing of instance resources in flatdata-py (#216)
Browse files Browse the repository at this point in the history
Fixes #128

Signed-off-by: Christian Vetter <[email protected]>
  • Loading branch information
VeaaC authored Nov 26, 2021
1 parent 32362b7 commit 1843c76
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
19 changes: 17 additions & 2 deletions flatdata-py/flatdata/lib/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ def __getitem__(self, index):
if isinstance(index, slice):
return _VectorSlice(index, self)

if index >= self._size:
raise IndexError("Vector access out of bounds")
if index < 0:
index += len(self)
if index >= self._size or index < 0:
raise IndexError("Vector access out of bounds")
return self._get_item(index)

def __iter__(self):
Expand Down Expand Up @@ -173,6 +173,21 @@ def __getitem__(self, item):


class Instance(ResourceBase):
def __getitem__(self, index):
if isinstance(index, slice):
raise IndexError("Instance has only one item")

if index < 0:
index += 1
if index >= 1 or index < 0:
raise IndexError("Instance access out of bounds")

return self._get_item(index)

def __iter__(self):
for i in range(1):
yield self._get_item(i)

def __getattr__(self, name):
offset = self._item_offset(0)
return getattr(self._element_type(self._mem, offset), name)
Expand Down
1 change: 1 addition & 0 deletions flatdata-py/tests/test_backward_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def test_instance_reading():
}
archive = module.backward_compatibility_Archive(DictResourceStorage(valid_data))
check_signed_struct(archive.resource)
check_signed_struct(archive.resource[0])


def test_vector_reading():
Expand Down

0 comments on commit 1843c76

Please sign in to comment.