Skip to content

Commit

Permalink
Support IPython auto-completion for group and channel names (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreeve committed Dec 2, 2021
1 parent ec4dd4d commit bf4d2f6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions nptdms/tdms.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ def __getitem__(self, group_name):
except KeyError:
raise KeyError("There is no group named '%s' in the TDMS file" % group_name)

def _ipython_key_completions_(self):
""" Return possible group names for tab-completion when indexing
"""
return list(self._groups.keys())

def __enter__(self):
return self

Expand Down Expand Up @@ -393,6 +398,11 @@ def __getitem__(self, channel_name):
"There is no channel named '%s' in group '%s' of the TDMS file" %
(channel_name, self.name))

def _ipython_key_completions_(self):
""" Return possible channel names for tab-completion when indexing
"""
return list(self._channels.keys())


class TdmsChannel(object):
""" Represents a data channel in a TDMS file.
Expand Down
20 changes: 20 additions & 0 deletions nptdms/test/test_tdms_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,3 +1036,23 @@ def test_no_new_obj_list_for_first_segment():
tdms_data = TdmsFile.read(temp_file.file)
channel_data = tdms_data['group']['channel1']
compare_arrays(channel_data[:], [1, 2, 3, 4, 5, 6, 7, 8])


def test_group_name_autocompletion():
test_file = GeneratedFile()
test_file.add_segment(*basic_segment())
tdms_data = test_file.load()

completion_options = tdms_data._ipython_key_completions_()
assert "Group" in completion_options


def test_channel_name_autocompletion():
test_file = GeneratedFile()
test_file.add_segment(*basic_segment())
tdms_data = test_file.load()

group = tdms_data["Group"]
completion_options = group._ipython_key_completions_()
assert "Channel1" in completion_options
assert "Channel2" in completion_options

0 comments on commit bf4d2f6

Please sign in to comment.