Skip to content

Commit 35a52c6

Browse files
authored
fix __repr__ and tests (#736)
1 parent 033d246 commit 35a52c6

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

src/pynwb/core.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from .form import Container, Data, DataRegion, get_region_slicer
77

88
from . import CORE_NAMESPACE, register_class
9-
from six import with_metaclass, iteritems
9+
from six import with_metaclass
1010

1111

1212
def _not_parent(arg):
@@ -145,17 +145,19 @@ def __gather_nwbfields(cls, name, bases, classdict):
145145
cls.__nwbfields__ = tuple(new_nwbfields)
146146

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

153-
def __smart_str(self, v):
154+
@staticmethod
155+
def __smart_str(v):
154156
"""
155157
Print compact string representation of data.
156158
157-
If v is a list, print it using numpy. This will condense the string
158-
representation of datasets with many elements.
159+
If v is a list, try to print it using numpy. This will condense the string
160+
representation of datasets with many elements. If that doesn't work, just print the list.
159161
160162
If v is a dictionary, print the name and type of each element
161163
@@ -172,10 +174,13 @@ def __smart_str(self, v):
172174
173175
"""
174176
if isinstance(v, list):
175-
return str(np.array(v))
177+
try:
178+
return str(np.array(v))
179+
except ValueError:
180+
return str(v)
176181
elif isinstance(v, dict):
177182
template = '{'
178-
keys = list(v.keys())
183+
keys = list(sorted(v.keys()))
179184
for k in keys[:-1]:
180185
template += " {} {}, ".format(k, type(v[k]))
181186
if keys:

tests/integration/ui_write/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ def roundtripContainer(self):
168168
def test_roundtrip(self):
169169
self.read_container = self.roundtripContainer()
170170
# make sure we get a completely new object
171+
str(self.container) # added as a test to make sure printing works
171172
self.assertNotEqual(id(self.container), id(self.read_container))
172173
self.assertContainerEqual(self.container, self.read_container)
173174

tests/unit/pynwb_tests/test_core.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,37 @@ def test_print_file(self):
281281
identifier='identifier', session_start_time=datetime.now(tzlocal()))
282282
ts = TimeSeries('name', [1., 2., 3.] * 1000, timestamps=[1, 2, 3])
283283
ts2 = TimeSeries('name2', [1, 2, 3] * 1000, timestamps=[1, 2, 3])
284-
print(ts)
284+
self.assertEqual(str(ts), """
285+
name <class 'pynwb.base.TimeSeries'>
286+
Fields:
287+
comments: no comments
288+
conversion: 1.0
289+
data: [1. 2. 3. ... 1. 2. 3.]
290+
description: no description
291+
interval: 1
292+
num_samples: 3000
293+
resolution: 0.0
294+
timestamps: [1 2 3]
295+
timestamps_unit: Seconds
296+
"""
297+
)
285298
nwbfile.add_acquisition(ts)
286299
nwbfile.add_acquisition(ts2)
287-
print(nwbfile)
300+
empty_set_str = str(set()) # changes between py2 and py3
301+
self.assertEqual(str(nwbfile),
302+
"""
303+
root <class 'pynwb.file.NWBFile'>
304+
Fields:
305+
acquisition: { name <class 'pynwb.base.TimeSeries'>, name2 <class 'pynwb.base.TimeSeries'> }
306+
analysis: { }
307+
devices: { }
308+
electrode_groups: { }
309+
epoch_tags: """ + empty_set_str + """
310+
ic_electrodes: { }
311+
imaging_planes: { }
312+
modules: { }
313+
ogen_sites: { }
314+
stimulus: { }
315+
stimulus_template: { }
316+
time_intervals: { }
317+
""")

0 commit comments

Comments
 (0)