Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dict-like methods for ProcessingModule #2020

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,5 @@ _version.py

.core_typemap_version
core_typemap.pkl

venv/
40 changes: 40 additions & 0 deletions src/pynwb/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,46 @@ def get_data_interface(self, **kwargs):
warn(PendingDeprecationWarning('get_data_interface will be replaced by get'))
return self.get(kwargs['data_interface_name'])

def __len__(self):
"""Get the number of data interfaces in this ProcessingModule.

Returns
-------
int
Number of data interfaces
"""
return len(self.data_interfaces)

def keys(self):
"""Get the names of data interfaces in this ProcessingModule.

Returns
-------
KeysView
View of interface names
"""
return self.data_interfaces.keys()

def values(self):
"""Get the data interfaces in this ProcessingModule.

Returns
-------
ValuesView
View of interfaces
"""
return self.data_interfaces.values()

def items(self):
"""Get the (name, interface) pairs in this ProcessingModule.

Returns
-------
ItemsView
View of (name, interface) pairs
"""
return self.data_interfaces.items()


@register_class('TimeSeries', CORE_NAMESPACE)
class TimeSeries(NWBDataInterface):
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,35 @@ def test_getitem(self):
tmp = self.pm["test_ts"]
self.assertIs(tmp, ts)

def test_len(self):
"""Test that len() returns number of data interfaces."""
self.assertEqual(len(self.pm), 0)
ts = self._create_time_series()
self.pm.add(ts)
self.assertEqual(len(self.pm), 1)
ts2 = TimeSeries(name="test_ts2", data=[1, 2, 3], unit="unit", rate=1.0)
self.pm.add(ts2)
self.assertEqual(len(self.pm), 2)

def test_dict_methods(self):
"""Test dictionary-like methods (keys, values, items)."""
ts = self._create_time_series()
ts2 = TimeSeries(name="test_ts2", data=[1, 2, 3], unit="unit", rate=1.0)
self.pm.add(ts)
self.pm.add(ts2)

# Test keys()
keys = self.pm.keys()
self.assertEqual(set(keys), {"test_ts", "test_ts2"})

# Test values()
values = self.pm.values()
self.assertEqual(set(values), {ts, ts2})

# Test items()
items = self.pm.items()
self.assertEqual(set(items), {("test_ts", ts), ("test_ts2", ts2)})


class TestTimeSeries(TestCase):
def test_init_no_parent(self):
Expand Down
Loading