Skip to content

Commit a629da2

Browse files
committed
implement setitem; fix test errors
1 parent 30e1ac2 commit a629da2

File tree

5 files changed

+31
-50
lines changed

5 files changed

+31
-50
lines changed

zarr/creation.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -289,20 +289,15 @@ def array(data, **kwargs):
289289
data = np.asanyarray(data)
290290

291291
# setup dtype
292-
dtype = kwargs.pop('dtype', None)
293-
if dtype is None:
294-
dtype = data.dtype
292+
kwargs.setdefault('dtype', data.dtype)
295293

296-
# setup shape
297-
shape = data.shape
298-
299-
# setup chunks
300-
chunks = kwargs.pop('chunks', None)
301-
if chunks is None:
302-
_, chunks = _get_shape_chunks(data)
294+
# setup shape and chunks
295+
shape, chunks = _get_shape_chunks(data)
296+
kwargs['shape'] = data.shape
297+
kwargs.setdefault('chunks', chunks)
303298

304299
# instantiate array
305-
z = create(shape=shape, chunks=chunks, dtype=dtype, **kwargs)
300+
z = create(**kwargs)
306301

307302
# fill with data
308303
z[:] = data

zarr/hierarchy.py

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

55

66
import numpy as np
@@ -18,7 +18,7 @@
1818
from zarr.meta import decode_group_metadata
1919

2020

21-
class Group(Mapping):
21+
class Group(MutableMapping):
2222
"""Instantiate a group from an initialized store.
2323
2424
Parameters
@@ -304,7 +304,7 @@ def __getitem__(self, item):
304304
raise KeyError(item)
305305

306306
def __setitem__(self, item, value):
307-
raise TypeError('item assignment not supported')
307+
self.array(item, value, overwrite=True)
308308

309309
def __delitem__(self, item):
310310
return self._write_op(self._delitem_nosync, item)
@@ -508,10 +508,7 @@ def require_groups(self, *names):
508508
"""Convenience method to require multiple groups in a single call."""
509509
return tuple(self.require_group(name) for name in names)
510510

511-
def create_dataset(self, name, data=None, shape=None, chunks=None,
512-
dtype=None, compressor='default', fill_value=0,
513-
order='C', synchronizer=None, filters=None,
514-
overwrite=False, cache_metadata=True, **kwargs):
511+
def create_dataset(self, name, **kwargs):
515512
"""Create an array.
516513
517514
Parameters
@@ -564,43 +561,23 @@ def create_dataset(self, name, data=None, shape=None, chunks=None,
564561
565562
""" # flake8: noqa
566563

567-
return self._write_op(self._create_dataset_nosync, name, data=data,
568-
shape=shape, chunks=chunks, dtype=dtype,
569-
compressor=compressor, fill_value=fill_value,
570-
order=order, synchronizer=synchronizer,
571-
filters=filters, overwrite=overwrite,
572-
cache_metadata=cache_metadata, **kwargs)
564+
return self._write_op(self._create_dataset_nosync, name, **kwargs)
573565

574-
def _create_dataset_nosync(self, name, data=None, shape=None, chunks=None,
575-
dtype=None, compressor='default',
576-
fill_value=0, order='C', synchronizer=None,
577-
filters=None, overwrite=False,
578-
cache_metadata=True, **kwargs):
566+
def _create_dataset_nosync(self, name, data=None, **kwargs):
579567

580568
path = self._item_path(name)
581569

582570
# determine synchronizer
583-
if synchronizer is None:
584-
synchronizer = self._synchronizer
571+
kwargs.setdefault('synchronizer', self._synchronizer)
585572

586573
# create array
587-
if data is not None:
588-
a = array(data, chunks=chunks, dtype=dtype,
589-
compressor=compressor, fill_value=fill_value,
590-
order=order, synchronizer=synchronizer,
591-
store=self._store, path=path,
592-
chunk_store=self._chunk_store, filters=filters,
593-
overwrite=overwrite, cache_metadata=cache_metadata,
594-
**kwargs)
574+
if data is None:
575+
a = create(store=self._store, path=path,
576+
chunk_store=self._chunk_store, **kwargs)
595577

596578
else:
597-
a = create(shape=shape, chunks=chunks, dtype=dtype,
598-
compressor=compressor, fill_value=fill_value,
599-
order=order, synchronizer=synchronizer,
600-
store=self._store, path=path,
601-
chunk_store=self._chunk_store, filters=filters,
602-
overwrite=overwrite, cache_metadata=cache_metadata,
603-
**kwargs)
579+
a = array(data, store=self._store, path=path,
580+
chunk_store=self._chunk_store, **kwargs)
604581

605582
return a
606583

zarr/storage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -891,8 +891,8 @@ def getsize(self, path=None):
891891
return info.compress_size
892892
except KeyError:
893893
err_path_not_found(path)
894-
else:
895-
return 0
894+
else:
895+
return 0
896896

897897

898898
def migrate_1to2(store):

zarr/tests/test_creation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ def test_array():
7272
eq(z.dtype, z2.dtype)
7373
assert_array_equal(z[:], z2[:])
7474

75+
# with chunky array-likes
76+
7577
b = np.arange(1000).reshape(100, 10)
7678
c = MockBcolzArray(b, 10)
7779
z3 = array(c)

zarr/tests/test_hierarchy.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,15 @@ def test_group_repr(self):
507507

508508
def test_setitem(self):
509509
g = self.create_group()
510-
with assert_raises(TypeError):
511-
g['foo'] = 'bar'
510+
try:
511+
data = np.arange(100)
512+
g['foo'] = data
513+
assert_array_equal(data, g['foo'])
514+
data = np.arange(200)
515+
g['foo'] = data
516+
assert_array_equal(data, g['foo'])
517+
except NotImplementedError:
518+
pass
512519

513520
def test_delitem(self):
514521
g = self.create_group()

0 commit comments

Comments
 (0)