Skip to content

Commit

Permalink
fix __repr__ and tests (#736)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendichter authored Nov 16, 2018
1 parent 033d246 commit 35a52c6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
23 changes: 14 additions & 9 deletions src/pynwb/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .form import Container, Data, DataRegion, get_region_slicer

from . import CORE_NAMESPACE, register_class
from six import with_metaclass, iteritems
from six import with_metaclass


def _not_parent(arg):
Expand Down Expand Up @@ -145,17 +145,19 @@ def __gather_nwbfields(cls, name, bases, classdict):
cls.__nwbfields__ = tuple(new_nwbfields)

def __repr__(self):
template = "{} {}\nFields:\n""".format(getattr(self, 'name'), type(self))
for k, v in iteritems(self.fields):
template += " {}: {} \n".format(k, self.__smart_str(v))
template = "\n{} {}\nFields:\n""".format(getattr(self, 'name'), type(self))
for k in sorted(self.fields): # sorted to enable tests
v = self.fields[k]
template += " {}: {}\n".format(k, self.__smart_str(v))
return template

def __smart_str(self, v):
@staticmethod
def __smart_str(v):
"""
Print compact string representation of data.
If v is a list, print it using numpy. This will condense the string
representation of datasets with many elements.
If v is a list, try to print it using numpy. This will condense the string
representation of datasets with many elements. If that doesn't work, just print the list.
If v is a dictionary, print the name and type of each element
Expand All @@ -172,10 +174,13 @@ def __smart_str(self, v):
"""
if isinstance(v, list):
return str(np.array(v))
try:
return str(np.array(v))
except ValueError:
return str(v)
elif isinstance(v, dict):
template = '{'
keys = list(v.keys())
keys = list(sorted(v.keys()))
for k in keys[:-1]:
template += " {} {}, ".format(k, type(v[k]))
if keys:
Expand Down
1 change: 1 addition & 0 deletions tests/integration/ui_write/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def roundtripContainer(self):
def test_roundtrip(self):
self.read_container = self.roundtripContainer()
# make sure we get a completely new object
str(self.container) # added as a test to make sure printing works
self.assertNotEqual(id(self.container), id(self.read_container))
self.assertContainerEqual(self.container, self.read_container)

Expand Down
34 changes: 32 additions & 2 deletions tests/unit/pynwb_tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,37 @@ def test_print_file(self):
identifier='identifier', session_start_time=datetime.now(tzlocal()))
ts = TimeSeries('name', [1., 2., 3.] * 1000, timestamps=[1, 2, 3])
ts2 = TimeSeries('name2', [1, 2, 3] * 1000, timestamps=[1, 2, 3])
print(ts)
self.assertEqual(str(ts), """
name <class 'pynwb.base.TimeSeries'>
Fields:
comments: no comments
conversion: 1.0
data: [1. 2. 3. ... 1. 2. 3.]
description: no description
interval: 1
num_samples: 3000
resolution: 0.0
timestamps: [1 2 3]
timestamps_unit: Seconds
"""
)
nwbfile.add_acquisition(ts)
nwbfile.add_acquisition(ts2)
print(nwbfile)
empty_set_str = str(set()) # changes between py2 and py3
self.assertEqual(str(nwbfile),
"""
root <class 'pynwb.file.NWBFile'>
Fields:
acquisition: { name <class 'pynwb.base.TimeSeries'>, name2 <class 'pynwb.base.TimeSeries'> }
analysis: { }
devices: { }
electrode_groups: { }
epoch_tags: """ + empty_set_str + """
ic_electrodes: { }
imaging_planes: { }
modules: { }
ogen_sites: { }
stimulus: { }
stimulus_template: { }
time_intervals: { }
""")

0 comments on commit 35a52c6

Please sign in to comment.