|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 | from __future__ import absolute_import, print_function, division
|
| 3 | +from collections import Mapping |
3 | 4 |
|
4 | 5 |
|
5 | 6 | import numpy as np
|
|
16 | 17 | from zarr.meta import decode_group_metadata
|
17 | 18 |
|
18 | 19 |
|
19 |
| -class Group(object): |
| 20 | +class Group(Mapping): |
20 | 21 | """Instantiate a group from an initialized store.
|
21 | 22 |
|
22 | 23 | Parameters
|
@@ -158,11 +159,15 @@ def __iter__(self):
|
158 | 159 | quux
|
159 | 160 |
|
160 | 161 | """
|
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 |
162 | 167 |
|
163 | 168 | def __len__(self):
|
164 | 169 | """Number of members."""
|
165 |
| - return sum(1 for _ in self.keys()) |
| 170 | + return sum(1 for _ in self) |
166 | 171 |
|
167 | 172 | def __repr__(self):
|
168 | 173 | r = '%s.%s(' % (type(self).__module__, type(self).__name__)
|
@@ -216,12 +221,8 @@ def __contains__(self, item):
|
216 | 221 |
|
217 | 222 | """
|
218 | 223 | 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) |
225 | 226 |
|
226 | 227 | def __getitem__(self, item):
|
227 | 228 | """Obtain a group member.
|
@@ -259,78 +260,6 @@ def __getitem__(self, item):
|
259 | 260 | else:
|
260 | 261 | raise KeyError(item)
|
261 | 262 |
|
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 |
| - |
334 | 263 | def group_keys(self):
|
335 | 264 | """Return an iterator over member names for groups only.
|
336 | 265 |
|
|
0 commit comments