Skip to content

Commit d084cb6

Browse files
committed
default fill_value 0; refactor errors; resolves #49
1 parent 18ead2b commit d084cb6

File tree

9 files changed

+99
-59
lines changed

9 files changed

+99
-59
lines changed

docs/tutorial.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ the delta filter::
230230
... chunks=(1000, 1000), compressor=compressor)
231231
>>> z
232232
Array((10000, 10000), int32, chunks=(1000, 1000), order=C)
233-
nbytes: 381.5M; nbytes_stored: 248.9K; ratio: 1569.6; initialized: 100/100
233+
nbytes: 381.5M; nbytes_stored: 248.9K; ratio: 1569.7; initialized: 100/100
234234
compressor: LZMA(format=1, check=-1, preset=None, filters=[{'dist': 4, 'id': 3}, {'preset': 1, 'id': 33}])
235235
store: dict
236236

@@ -327,7 +327,7 @@ provided that all processes have access to a shared file system. E.g.::
327327
... synchronizer=synchronizer)
328328
>>> z
329329
Array((10000, 10000), int32, chunks=(1000, 1000), order=C)
330-
nbytes: 381.5M; nbytes_stored: 326; ratio: 1226993.9; initialized: 0/100
330+
nbytes: 381.5M; nbytes_stored: 323; ratio: 1238390.1; initialized: 0/100
331331
compressor: Blosc(cname='lz4', clevel=5, shuffle=1)
332332
store: DirectoryStore; synchronizer: ProcessSynchronizer
333333

zarr/core.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from zarr.storage import array_meta_key, attrs_key, listdir, getsize
1414
from zarr.meta import decode_array_metadata, encode_array_metadata
1515
from zarr.attrs import Attributes
16-
from zarr.errors import PermissionError
16+
from zarr.errors import PermissionError, err_read_only, err_array_not_found
1717
from zarr.compat import reduce
1818
from zarr.codecs import get_codec
1919

@@ -108,7 +108,7 @@ def _load_metadata(self):
108108
mkey = self._key_prefix + array_meta_key
109109
meta_bytes = self._store[mkey]
110110
except KeyError:
111-
raise ValueError('store has no metadata')
111+
err_array_not_found(self._path)
112112
else:
113113

114114
# decode and store metadata
@@ -135,7 +135,7 @@ def _load_metadata(self):
135135

136136
def _flush_metadata(self):
137137
if self._is_view:
138-
raise PermissionError('operation not permitted for views')
138+
raise PermissionError('not permitted for views')
139139

140140
if self._compressor:
141141
compressor_config = self._compressor.get_config()
@@ -496,7 +496,7 @@ def __setitem__(self, key, value):
496496

497497
# guard conditions
498498
if self._read_only:
499-
raise PermissionError('array is read-only')
499+
err_read_only()
500500

501501
# refresh metadata
502502
if not self._cache_metadata:
@@ -790,7 +790,7 @@ def _write_op(self, f, *args, **kwargs):
790790

791791
# guard condition
792792
if self._read_only:
793-
raise PermissionError('array is read-only')
793+
err_read_only()
794794

795795
# synchronization
796796
if self._synchronizer is None:
@@ -1057,7 +1057,7 @@ def view(self, shape=None, chunks=None, dtype=None,
10571057
... v.resize(20000)
10581058
... except PermissionError as e:
10591059
... print(e)
1060-
operation not permitted for views
1060+
not permitted for views
10611061
10621062
""" # flake8: noqa
10631063

zarr/creation.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88

99
from zarr.core import Array
1010
from zarr.storage import DirectoryStore, init_array, contains_array, \
11-
contains_group, default_compressor, _require_parent_group
11+
contains_group, default_compressor, normalize_storage_path
1212
from zarr.codecs import codec_registry
13+
from zarr.errors import err_contains_array, err_contains_group, \
14+
err_array_not_found
1315

1416

1517
def create(shape, chunks=None, dtype=None, compressor='default',
16-
fill_value=None, order='C', store=None, synchronizer=None,
18+
fill_value=0, order='C', store=None, synchronizer=None,
1719
overwrite=False, path=None, chunk_store=None, filters=None,
1820
cache_metadata=True, **kwargs):
1921
"""Create an array.
@@ -65,7 +67,7 @@ def create(shape, chunks=None, dtype=None, compressor='default',
6567
>>> z = zarr.create((10000, 10000), chunks=(1000, 1000))
6668
>>> z
6769
Array((10000, 10000), float64, chunks=(1000, 1000), order=C)
68-
nbytes: 762.9M; nbytes_stored: 326; ratio: 2453987.7; initialized: 0/100
70+
nbytes: 762.9M; nbytes_stored: 323; ratio: 2476780.2; initialized: 0/100
6971
compressor: Blosc(cname='lz4', clevel=5, shuffle=1)
7072
store: dict
7173
@@ -295,7 +297,7 @@ def array(data, **kwargs):
295297

296298

297299
def open_array(store=None, mode='a', shape=None, chunks=None, dtype=None,
298-
compressor='default', fill_value=None, order='C',
300+
compressor='default', fill_value=0, order='C',
299301
synchronizer=None, filters=None, cache_metadata=True,
300302
path=None, **kwargs):
301303
"""Open array using mode-like semantics.
@@ -374,6 +376,7 @@ def open_array(store=None, mode='a', shape=None, chunks=None, dtype=None,
374376

375377
# handle polymorphic store arg
376378
store = _handle_store_arg(store)
379+
path = normalize_storage_path(path)
377380

378381
# compatibility
379382
compressor, fill_value = _handle_kwargs(compressor, fill_value, kwargs)
@@ -382,9 +385,9 @@ def open_array(store=None, mode='a', shape=None, chunks=None, dtype=None,
382385

383386
if mode in ['r', 'r+']:
384387
if contains_group(store, path=path):
385-
raise ValueError('store contains group')
388+
err_contains_group(path)
386389
elif not contains_array(store, path=path):
387-
raise ValueError('array does not exist')
390+
err_array_not_found(path)
388391

389392
elif mode == 'w':
390393
init_array(store, shape=shape, chunks=chunks, dtype=dtype,
@@ -393,17 +396,17 @@ def open_array(store=None, mode='a', shape=None, chunks=None, dtype=None,
393396

394397
elif mode == 'a':
395398
if contains_group(store, path=path):
396-
raise ValueError('store contains group')
399+
err_contains_group(path)
397400
elif not contains_array(store, path=path):
398401
init_array(store, shape=shape, chunks=chunks, dtype=dtype,
399402
compressor=compressor, fill_value=fill_value,
400403
order=order, filters=filters, path=path)
401404

402405
elif mode in ['w-', 'x']:
403406
if contains_group(store, path=path):
404-
raise ValueError('store contains group')
407+
err_contains_group(path)
405408
elif contains_array(store, path=path):
406-
raise ValueError('store contains array')
409+
err_contains_array(path)
407410
else:
408411
init_array(store, shape=shape, chunks=chunks, dtype=dtype,
409412
compressor=compressor, fill_value=fill_value,

zarr/errors.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,36 @@ class PermissionError(Exception):
1717

1818
class MetadataError(Exception):
1919
pass
20+
21+
22+
def err_contains_group(path):
23+
raise KeyError('path %r contains a group' % path)
24+
25+
26+
def err_contains_array(path):
27+
raise KeyError('path %r contains an array' % path)
28+
29+
30+
def err_array_not_found(path):
31+
raise KeyError('array not found at path %r' % path)
32+
33+
34+
def err_group_not_found(path):
35+
raise KeyError('group not found at path %r' % path)
36+
37+
38+
def err_path_not_found(path):
39+
raise KeyError('path %r not found' % path)
40+
41+
42+
def err_bad_compressor(compressor):
43+
raise ValueError('bad compressor; expected Codec object, found %r' %
44+
compressor)
45+
46+
47+
def err_fspath_exists_notdir(fspath):
48+
raise ValueError('path exists but is not a directory: %r' % fspath)
49+
50+
51+
def err_read_only():
52+
raise PermissionError('object is read-only')

zarr/hierarchy.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
from zarr.attrs import Attributes
1010
from zarr.core import Array
1111
from zarr.storage import contains_array, contains_group, init_group, \
12-
DictStore, DirectoryStore, group_meta_key, attrs_key, listdir, rmdir
12+
DictStore, DirectoryStore, group_meta_key, attrs_key, listdir
1313
from zarr.creation import array, create, empty, zeros, ones, full, \
1414
empty_like, zeros_like, ones_like, full_like
1515
from zarr.util import normalize_storage_path, normalize_shape
16-
from zarr.errors import PermissionError
16+
from zarr.errors import PermissionError, err_contains_array, \
17+
err_contains_group, err_group_not_found, err_read_only
1718
from zarr.meta import decode_group_metadata
1819

1920

@@ -91,14 +92,14 @@ def __init__(self, store, path=None, read_only=False, chunk_store=None,
9192

9293
# guard conditions
9394
if contains_array(store, path=self._path):
94-
raise ValueError('store contains an array')
95+
err_contains_array(path)
9596

9697
# initialize metadata
9798
try:
9899
mkey = self._key_prefix + group_meta_key
99100
meta_bytes = store[mkey]
100101
except KeyError:
101-
raise ValueError('store has no metadata')
102+
err_group_not_found(path)
102103
else:
103104
meta = decode_group_metadata(meta_bytes)
104105
self._meta = meta
@@ -285,7 +286,7 @@ def __getitem__(self, item):
285286
store: DictStore
286287
>>> g1['foo/bar/baz']
287288
Array(/foo/bar/baz, (100,), float64, chunks=(10,), order=C)
288-
nbytes: 800; nbytes_stored: 293; ratio: 2.7; initialized: 0/10
289+
nbytes: 800; nbytes_stored: 290; ratio: 2.8; initialized: 0/10
289290
compressor: Blosc(cname='lz4', clevel=5, shuffle=1)
290291
store: DictStore
291292
@@ -396,7 +397,7 @@ def _write_op(self, f, *args, **kwargs):
396397

397398
# guard condition
398399
if self._read_only:
399-
raise PermissionError('group is read-only')
400+
err_read_only()
400401

401402
# synchronization
402403
if self._synchronizer is None:
@@ -494,7 +495,7 @@ def require_groups(self, *names):
494495
return tuple(self.require_group(name) for name in names)
495496

496497
def create_dataset(self, name, data=None, shape=None, chunks=None,
497-
dtype=None, compressor='default', fill_value=None,
498+
dtype=None, compressor='default', fill_value=0,
498499
order='C', synchronizer=None, filters=None,
499500
overwrite=False, cache_metadata=True, **kwargs):
500501
"""Create an array.
@@ -543,7 +544,7 @@ def create_dataset(self, name, data=None, shape=None, chunks=None,
543544
... chunks=(1000, 1000))
544545
>>> d1
545546
Array(/foo, (10000, 10000), float64, chunks=(1000, 1000), order=C)
546-
nbytes: 762.9M; nbytes_stored: 326; ratio: 2453987.7; initialized: 0/100
547+
nbytes: 762.9M; nbytes_stored: 323; ratio: 2476780.2; initialized: 0/100
547548
compressor: Blosc(cname='lz4', clevel=5, shuffle=1)
548549
store: DictStore
549550
@@ -558,7 +559,7 @@ def create_dataset(self, name, data=None, shape=None, chunks=None,
558559

559560
def _create_dataset_nosync(self, name, data=None, shape=None, chunks=None,
560561
dtype=None, compressor='default',
561-
fill_value=None, order='C', synchronizer=None,
562+
fill_value=0, order='C', synchronizer=None,
562563
filters=None, overwrite=False,
563564
cache_metadata=True, **kwargs):
564565

@@ -804,6 +805,7 @@ def group(store=None, overwrite=False, chunk_store=None, synchronizer=None,
804805

805806
# handle polymorphic store arg
806807
store = _handle_store_arg(store)
808+
path = normalize_storage_path(path)
807809

808810
# require group
809811
if overwrite or not contains_group(store):
@@ -857,29 +859,30 @@ def open_group(store=None, mode='a', synchronizer=None, path=None):
857859

858860
# handle polymorphic store arg
859861
store = _handle_store_arg(store)
862+
path = normalize_storage_path(path)
860863

861864
# ensure store is initialized
862865

863866
if mode in ['r', 'r+']:
864867
if contains_array(store, path=path):
865-
raise ValueError('store contains array')
868+
err_contains_array(path)
866869
elif not contains_group(store, path=path):
867-
raise ValueError('group does not exist')
870+
err_group_not_found(path)
868871

869872
elif mode == 'w':
870873
init_group(store, overwrite=True, path=path)
871874

872875
elif mode == 'a':
873876
if contains_array(store, path=path):
874-
raise ValueError('store contains array')
877+
err_contains_array(path)
875878
if not contains_group(store, path=path):
876879
init_group(store, path=path)
877880

878881
elif mode in ['w-', 'x']:
879882
if contains_array(store, path=path):
880-
raise ValueError('store contains array')
883+
err_contains_array(path)
881884
elif contains_group(store, path=path):
882-
raise ValueError('store contains group')
885+
err_contains_group(path)
883886
else:
884887
init_group(store, path=path)
885888

zarr/storage.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
from zarr.meta import encode_array_metadata, encode_group_metadata
1717
from zarr.compat import PY2, binary_type
1818
from zarr.codecs import codec_registry
19-
from zarr.errors import PermissionError
19+
from zarr.errors import PermissionError, err_contains_group, \
20+
err_contains_array, err_path_not_found, err_bad_compressor, \
21+
err_fspath_exists_notdir, err_read_only
2022

2123

2224
array_meta_key = '.zarray'
@@ -267,9 +269,9 @@ def _init_array_metadata(store, shape, chunks, dtype=None,
267269
if chunk_store is not None and chunk_store != store:
268270
rmdir(chunk_store, path)
269271
elif contains_array(store, path):
270-
raise KeyError('path %r contains an array' % path)
272+
err_contains_array(path)
271273
elif contains_group(store, path):
272-
raise KeyError('path %r contains a group' % path)
274+
err_contains_group(path)
273275

274276
# normalize metadata
275277
shape = normalize_shape(shape)
@@ -287,8 +289,7 @@ def _init_array_metadata(store, shape, chunks, dtype=None,
287289
try:
288290
compressor_config = compressor.get_config()
289291
except AttributeError:
290-
raise ValueError('bad compressor argument; expected Codec object, '
291-
'found %r' % compressor)
292+
err_bad_compressor(compressor)
292293
else:
293294
compressor_config = None
294295

@@ -352,9 +353,9 @@ def _init_group_metadata(store, overwrite=False, path=None, chunk_store=None):
352353
if chunk_store is not None and chunk_store != store:
353354
rmdir(chunk_store, path)
354355
elif contains_array(store, path):
355-
raise KeyError('path %r contains an array' % path)
356+
err_contains_array(path)
356357
elif contains_group(store, path):
357-
raise KeyError('path %r contains a group' % path)
358+
err_contains_group(path)
358359

359360
# initialize metadata
360361
# N.B., currently no metadata properties are needed, however there may
@@ -532,7 +533,7 @@ def getsize(self, path=None):
532533
parent, key = self._get_parent(path)
533534
value = parent[key]
534535
except KeyError:
535-
raise KeyError('path %r not found' % path)
536+
err_path_not_found(path)
536537
else:
537538
value = self.root
538539

@@ -597,7 +598,7 @@ def __init__(self, path):
597598
# guard conditions
598599
path = os.path.abspath(path)
599600
if os.path.exists(path) and not os.path.isdir(path):
600-
raise ValueError('path exists but is not a directory')
601+
err_fspath_exists_notdir(path)
601602

602603
self.path = path
603604

@@ -713,7 +714,7 @@ def getsize(self, path=None):
713714
size += os.path.getsize(child_fs_path)
714715
return size
715716
else:
716-
raise KeyError('path %r not found' % path)
717+
err_path_not_found(path)
717718

718719

719720
# noinspection PyPep8Naming
@@ -776,7 +777,7 @@ def __getitem__(self, key):
776777

777778
def __setitem__(self, key, value):
778779
if self.mode == 'r':
779-
raise PermissionError('mapping is read-only')
780+
err_read_only()
780781
value = ensure_bytes(value)
781782
with zipfile.ZipFile(self.path, mode='a',
782783
compression=self.compression,
@@ -846,7 +847,7 @@ def getsize(self, path=None):
846847
info = zf.getinfo(path)
847848
return info.compress_size
848849
except KeyError:
849-
raise KeyError('path %r not found' % path)
850+
err_path_not_found(path)
850851
else:
851852
return 0
852853

0 commit comments

Comments
 (0)