Skip to content

Commit 8721a52

Browse files
jakirkhamjrbourbeau
authored andcommitted
Refactor out _tofile/_fromfile from DirectoryStore (#503)
* Add `_fromfile` and `_tofile` methods To make it easier to sub out different ways of reading and writing data, create to methods to handle reading and writing directly. This way users don't need to reimplement all of the same logic that `__getitem__` and `__setitem__` are doing and can focus on simply reading and writing. Can be useful for trying memmaping for example. * Use _fromfile/_tofile in __getitem__/__set item__ * Update zarr/storage.py Co-Authored-By: James Bourbeau <jrbourbeau@users.noreply.github.com> * Add docstrings to `_fromfile` and `_tofile` * No need to return in `_tofile` * Adds release notes entry
1 parent 59df302 commit 8721a52

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

docs/release.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Upcoming Release
4040
* Use ``math.ceil`` for scalars.
4141
By :user:`John Kirkham <jakirkham>`; :issue:`500`.
4242

43+
* Refactor out ``_tofile``/``_fromfile`` from ``DirectoryStore``.
44+
By :user:`John Kirkham <jakirkham>`; :issue:`503`.
45+
4346
.. _release_2.3.2:
4447

4548
2.3.2

zarr/storage.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -746,12 +746,46 @@ def __init__(self, path, normalize_keys=False):
746746
def _normalize_key(self, key):
747747
return key.lower() if self.normalize_keys else key
748748

749+
def _fromfile(self, fn):
750+
""" Read data from a file
751+
752+
Parameters
753+
----------
754+
fn: str
755+
Filepath to open and read from.
756+
757+
Notes
758+
-----
759+
Subclasses should overload this method to specify any custom
760+
file reading logic.
761+
"""
762+
with open(fn, 'rb') as f:
763+
return f.read()
764+
765+
def _tofile(self, a, fn):
766+
""" Write data to a file
767+
768+
Parameters
769+
----------
770+
a: array-like
771+
Data to write into the file.
772+
773+
fn: str
774+
Filepath to open and write to.
775+
776+
Notes
777+
-----
778+
Subclasses should overload this method to specify any custom
779+
file writing logic.
780+
"""
781+
with open(fn, mode='wb') as f:
782+
f.write(a)
783+
749784
def __getitem__(self, key):
750785
key = self._normalize_key(key)
751786
filepath = os.path.join(self.path, key)
752787
if os.path.isfile(filepath):
753-
with open(filepath, 'rb') as f:
754-
return f.read()
788+
return self._fromfile(filepath)
755789
else:
756790
raise KeyError(key)
757791

@@ -784,8 +818,7 @@ def __setitem__(self, key, value):
784818
temp_name = file_name + '.' + uuid.uuid4().hex + '.partial'
785819
temp_path = os.path.join(dir_path, temp_name)
786820
try:
787-
with open(temp_path, mode='wb') as f:
788-
f.write(value)
821+
self._tofile(value, temp_path)
789822

790823
# move temporary file into place
791824
replace(temp_path, file_path)

0 commit comments

Comments
 (0)