Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions zarr/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import absolute_import, print_function, division
import operator
import itertools
import re


import numpy as np
Expand Down Expand Up @@ -340,8 +341,12 @@ def nchunks(self):
def nchunks_initialized(self):
"""The number of chunks that have been initialized with some data."""
# TODO fix bug here, need to only count chunks
return sum(1 for k in listdir(self.chunk_store, self._path)
if k not in [array_meta_key, attrs_key])

# key pattern for chunk keys
prog = re.compile(r'\.'.join([r'\d+'] * min(1, self.ndim)))

# count chunk keys
return sum(1 for k in listdir(self.chunk_store, self._path) if prog.match(k))

# backwards compability
initialized = nchunks_initialized
Expand Down
27 changes: 16 additions & 11 deletions zarr/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,17 +641,22 @@ def __setitem__(self, key, value):
raise KeyError(key)

# write to temporary file
with tempfile.NamedTemporaryFile(mode='wb', delete=False,
dir=dir_path,
prefix=file_name + '.',
suffix='.partial') as f:
f.write(value)
temp_path = f.name

# move temporary file into place
if os.path.exists(file_path):
os.remove(file_path)
os.rename(temp_path, file_path)
temp_path = None
try:
with tempfile.NamedTemporaryFile(mode='wb', delete=False, dir=dir_path,
prefix=file_name + '.', suffix='.partial') as f:
temp_path = f.name
f.write(value)

# move temporary file into place
if os.path.exists(file_path):
os.remove(file_path)
os.rename(temp_path, file_path)

finally:
# clean up if temp file still exists for whatever reason
if temp_path is not None and os.path.exists(temp_path):
os.remove(temp_path)

def __delitem__(self, key):
path = os.path.join(self.path, key)
Expand Down
10 changes: 10 additions & 0 deletions zarr/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,16 @@ def test_array_0d(self):
with assert_raises(ValueError):
z[...] = np.array([1, 2, 3])

def test_nchunks_initialized(self):

z = self.create_array(shape=100, chunks=10)
eq(0, z.nchunks_initialized)
# manually put something into the store to confuse matters
z.store['foo'] = b'bar'
eq(0, z.nchunks_initialized)
z[:] = 42
eq(10, z.nchunks_initialized)


class TestArrayWithPath(TestArray):

Expand Down