Skip to content

Commit 5b88b6c

Browse files
committed
Group extends Mapping
1 parent e34007f commit 5b88b6c

File tree

2 files changed

+11
-82
lines changed

2 files changed

+11
-82
lines changed

zarr/hierarchy.py

Lines changed: 10 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import absolute_import, print_function, division
3+
from collections import Mapping
34

45

56
import numpy as np
@@ -16,7 +17,7 @@
1617
from zarr.meta import decode_group_metadata
1718

1819

19-
class Group(object):
20+
class Group(Mapping):
2021
"""Instantiate a group from an initialized store.
2122
2223
Parameters
@@ -158,11 +159,15 @@ def __iter__(self):
158159
quux
159160
160161
"""
161-
return self.keys()
162+
for key in sorted(listdir(self.store, self.path)):
163+
path = self.path + '/' + key
164+
if (contains_array(self.store, path) or
165+
contains_group(self.store, path)):
166+
yield key
162167

163168
def __len__(self):
164169
"""Number of members."""
165-
return sum(1 for _ in self.keys())
170+
return sum(1 for _ in self)
166171

167172
def __repr__(self):
168173
r = '%s.%s(' % (type(self).__module__, type(self).__name__)
@@ -216,12 +221,8 @@ def __contains__(self, item):
216221
217222
"""
218223
path = self._item_path(item)
219-
if contains_array(self.store, path):
220-
return True
221-
elif contains_group(self.store, path):
222-
return True
223-
else:
224-
return False
224+
return contains_array(self.store, path) or \
225+
contains_group(self.store, path)
225226

226227
def __getitem__(self, item):
227228
"""Obtain a group member.
@@ -259,78 +260,6 @@ def __getitem__(self, item):
259260
else:
260261
raise KeyError(item)
261262

262-
def __setitem__(self, key, value):
263-
"""Not implemented."""
264-
raise NotImplementedError()
265-
266-
def keys(self):
267-
"""Return an iterator over member names.
268-
269-
Examples
270-
--------
271-
>>> import zarr
272-
>>> g1 = zarr.group()
273-
>>> g2 = g1.create_group('foo')
274-
>>> g3 = g1.create_group('bar')
275-
>>> d1 = g1.create_dataset('baz', shape=100, chunks=10)
276-
>>> d2 = g1.create_dataset('quux', shape=200, chunks=20)
277-
>>> sorted(g1.keys())
278-
['bar', 'baz', 'foo', 'quux']
279-
280-
"""
281-
for key in sorted(listdir(self.store, self.path)):
282-
path = self.path + '/' + key
283-
if (contains_array(self.store, path) or
284-
contains_group(self.store, path)):
285-
yield key
286-
287-
def values(self):
288-
"""Return an iterator over members.
289-
290-
Examples
291-
--------
292-
>>> import zarr
293-
>>> g1 = zarr.group()
294-
>>> g2 = g1.create_group('foo')
295-
>>> g3 = g1.create_group('bar')
296-
>>> d1 = g1.create_dataset('baz', shape=100, chunks=10)
297-
>>> d2 = g1.create_dataset('quux', shape=200, chunks=20)
298-
>>> for v in g1.values():
299-
... print(type(v), v.path)
300-
<class 'zarr.hierarchy.Group'> bar
301-
<class 'zarr.core.Array'> baz
302-
<class 'zarr.hierarchy.Group'> foo
303-
<class 'zarr.core.Array'> quux
304-
305-
"""
306-
return (v for _, v in self.items())
307-
308-
def items(self):
309-
"""Return an iterator over (name, value) pairs for all members.
310-
311-
Examples
312-
--------
313-
>>> import zarr
314-
>>> g1 = zarr.group()
315-
>>> g2 = g1.create_group('foo')
316-
>>> g3 = g1.create_group('bar')
317-
>>> d1 = g1.create_dataset('baz', shape=100, chunks=10)
318-
>>> d2 = g1.create_dataset('quux', shape=200, chunks=20)
319-
>>> for n, v in g1.items():
320-
... print(n, type(v))
321-
bar <class 'zarr.hierarchy.Group'>
322-
baz <class 'zarr.core.Array'>
323-
foo <class 'zarr.hierarchy.Group'>
324-
quux <class 'zarr.core.Array'>
325-
326-
"""
327-
for key in sorted(listdir(self.store, self.path)):
328-
path = self.path + '/' + key
329-
if contains_array(self.store, path):
330-
yield key, Array(self.store, path=path, readonly=self.readonly)
331-
elif contains_group(self.store, path):
332-
yield key, Group(self.store, path=path, readonly=self.readonly)
333-
334263
def group_keys(self):
335264
"""Return an iterator over member names for groups only.
336265

zarr/tests/test_hierarchy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ def test_setitem(self):
445445
store = self.create_store()
446446
init_group(store)
447447
g = Group(store=store)
448-
with assert_raises(NotImplementedError):
448+
with assert_raises(TypeError):
449449
g['foo'] = 'bar'
450450

451451
def test_array_creation(self):

0 commit comments

Comments
 (0)