|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | from __future__ import absolute_import, print_function, division |
3 | | -from collections import Mapping |
| 3 | +from collections import MutableMapping |
4 | 4 |
|
5 | 5 |
|
6 | 6 | import numpy as np |
|
9 | 9 | from zarr.attrs import Attributes |
10 | 10 | from zarr.core import Array |
11 | 11 | from zarr.storage import contains_array, contains_group, init_group, \ |
12 | | - DictStore, DirectoryStore, group_meta_key, attrs_key, listdir |
| 12 | + DictStore, DirectoryStore, group_meta_key, attrs_key, listdir, rmdir |
13 | 13 | from zarr.creation import array, create, empty, zeros, ones, full, \ |
14 | 14 | empty_like, zeros_like, ones_like, full_like |
15 | 15 | from zarr.util import normalize_storage_path, normalize_shape |
|
18 | 18 | from zarr.meta import decode_group_metadata |
19 | 19 |
|
20 | 20 |
|
21 | | -class Group(Mapping): |
| 21 | +class Group(MutableMapping): |
22 | 22 | """Instantiate a group from an initialized store. |
23 | 23 |
|
24 | 24 | Parameters |
@@ -303,6 +303,20 @@ def __getitem__(self, item): |
303 | 303 | else: |
304 | 304 | raise KeyError(item) |
305 | 305 |
|
| 306 | + def __setitem__(self, item, value): |
| 307 | + self.array(item, value, overwrite=True) |
| 308 | + |
| 309 | + def __delitem__(self, item): |
| 310 | + return self._write_op(self._delitem_nosync, item) |
| 311 | + |
| 312 | + def _delitem_nosync(self, item): |
| 313 | + path = self._item_path(item) |
| 314 | + if contains_array(self._store, path) or \ |
| 315 | + contains_group(self._store, path): |
| 316 | + rmdir(self._store, path) |
| 317 | + else: |
| 318 | + raise KeyError(item) |
| 319 | + |
306 | 320 | def group_keys(self): |
307 | 321 | """Return an iterator over member names for groups only. |
308 | 322 |
|
@@ -494,10 +508,7 @@ def require_groups(self, *names): |
494 | 508 | """Convenience method to require multiple groups in a single call.""" |
495 | 509 | return tuple(self.require_group(name) for name in names) |
496 | 510 |
|
497 | | - def create_dataset(self, name, data=None, shape=None, chunks=None, |
498 | | - dtype=None, compressor='default', fill_value=0, |
499 | | - order='C', synchronizer=None, filters=None, |
500 | | - overwrite=False, cache_metadata=True, **kwargs): |
| 511 | + def create_dataset(self, name, **kwargs): |
501 | 512 | """Create an array. |
502 | 513 |
|
503 | 514 | Parameters |
@@ -550,43 +561,23 @@ def create_dataset(self, name, data=None, shape=None, chunks=None, |
550 | 561 |
|
551 | 562 | """ # flake8: noqa |
552 | 563 |
|
553 | | - return self._write_op(self._create_dataset_nosync, name, data=data, |
554 | | - shape=shape, chunks=chunks, dtype=dtype, |
555 | | - compressor=compressor, fill_value=fill_value, |
556 | | - order=order, synchronizer=synchronizer, |
557 | | - filters=filters, overwrite=overwrite, |
558 | | - cache_metadata=cache_metadata, **kwargs) |
| 564 | + return self._write_op(self._create_dataset_nosync, name, **kwargs) |
559 | 565 |
|
560 | | - def _create_dataset_nosync(self, name, data=None, shape=None, chunks=None, |
561 | | - dtype=None, compressor='default', |
562 | | - fill_value=0, order='C', synchronizer=None, |
563 | | - filters=None, overwrite=False, |
564 | | - cache_metadata=True, **kwargs): |
| 566 | + def _create_dataset_nosync(self, name, data=None, **kwargs): |
565 | 567 |
|
566 | 568 | path = self._item_path(name) |
567 | 569 |
|
568 | 570 | # determine synchronizer |
569 | | - if synchronizer is None: |
570 | | - synchronizer = self._synchronizer |
| 571 | + kwargs.setdefault('synchronizer', self._synchronizer) |
571 | 572 |
|
572 | 573 | # create array |
573 | | - if data is not None: |
574 | | - a = array(data, chunks=chunks, dtype=dtype, |
575 | | - compressor=compressor, fill_value=fill_value, |
576 | | - order=order, synchronizer=synchronizer, |
577 | | - store=self._store, path=path, |
578 | | - chunk_store=self._chunk_store, filters=filters, |
579 | | - overwrite=overwrite, cache_metadata=cache_metadata, |
580 | | - **kwargs) |
| 574 | + if data is None: |
| 575 | + a = create(store=self._store, path=path, |
| 576 | + chunk_store=self._chunk_store, **kwargs) |
581 | 577 |
|
582 | 578 | else: |
583 | | - a = create(shape=shape, chunks=chunks, dtype=dtype, |
584 | | - compressor=compressor, fill_value=fill_value, |
585 | | - order=order, synchronizer=synchronizer, |
586 | | - store=self._store, path=path, |
587 | | - chunk_store=self._chunk_store, filters=filters, |
588 | | - overwrite=overwrite, cache_metadata=cache_metadata, |
589 | | - **kwargs) |
| 579 | + a = array(data, store=self._store, path=path, |
| 580 | + chunk_store=self._chunk_store, **kwargs) |
590 | 581 |
|
591 | 582 | return a |
592 | 583 |
|
|
0 commit comments