diff --git a/src/pynwb/core.py b/src/pynwb/core.py index 128628da9..f7ec23726 100644 --- a/src/pynwb/core.py +++ b/src/pynwb/core.py @@ -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): @@ -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 @@ -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: diff --git a/tests/integration/ui_write/base.py b/tests/integration/ui_write/base.py index d25c67dd0..2fa7f415c 100644 --- a/tests/integration/ui_write/base.py +++ b/tests/integration/ui_write/base.py @@ -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) diff --git a/tests/unit/pynwb_tests/test_core.py b/tests/unit/pynwb_tests/test_core.py index 363928d36..8c42cb76b 100644 --- a/tests/unit/pynwb_tests/test_core.py +++ b/tests/unit/pynwb_tests/test_core.py @@ -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 +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 +Fields: + acquisition: { name , name2 } + analysis: { } + devices: { } + electrode_groups: { } + epoch_tags: """ + empty_set_str + """ + ic_electrodes: { } + imaging_planes: { } + modules: { } + ogen_sites: { } + stimulus: { } + stimulus_template: { } + time_intervals: { } +""")