Skip to content

Commit

Permalink
Support negative integer indexing into channels (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreeve committed Jan 10, 2021
1 parent d30f3a9 commit a359785
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion nptdms/tdms.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,8 +719,11 @@ def _read_slice(self, start, stop, step):
return read_data[::step]

def _read_at_index(self, index):
orig_index = index
if index < 0:
index = self._length + index
if index < 0 or index >= self._length:
raise IndexError("Index {0} is outside of the channel bounds [0, {1}]".format(index, self._length - 1))
raise IndexError("Index {0} is outside of the channel bounds [0, {1}]".format(orig_index, self._length - 1))

if self._cached_chunk is not None:
# Check if we've already read and cached the chunk containing this index
Expand Down
12 changes: 12 additions & 0 deletions nptdms/test/test_tdms_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,18 @@ def test_indexing_channel_with_integer(index):
assert channel_object[index] == expected_channel_data[index]


@given(index=strategies.integers(-8, -1))
def test_indexing_channel_with_negative_integer(index):
""" Test indexing into a channel with a negative integer index
"""
test_file, expected_data = scenarios.chunked_segment().values
with test_file.get_tempfile() as temp_file:
with TdmsFile.open(temp_file.file) as tdms_file:
for ((group, channel), expected_channel_data) in expected_data.items():
channel_object = tdms_file[group][channel]
assert channel_object[index] == expected_channel_data[index]


def test_indexing_channel_with_integer_and_caching():
""" Test indexing into a channel with an integer index, reusing the same file to test caching
"""
Expand Down

0 comments on commit a359785

Please sign in to comment.