Skip to content

Commit 063a08f

Browse files
committed
Allow pathlib use in convenience and filesystem stores
1 parent 7cf6f80 commit 063a08f

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

zarr/convenience.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Convenience functions for storing and loading data."""
22
import io
33
import itertools
4+
import os
45
import re
56
from collections.abc import Mapping
67

@@ -100,6 +101,10 @@ def open(store=None, mode='a', **kwargs):
100101
raise PathNotFoundError(path)
101102

102103

104+
def _might_close(path):
105+
return isinstance(path, (str, os.PathLike))
106+
107+
103108
def save_array(store, arr, **kwargs):
104109
"""Convenience function to save a NumPy array to the local file system, following a
105110
similar API to the NumPy save() function.
@@ -131,7 +136,7 @@ def save_array(store, arr, **kwargs):
131136
array([ 0, 1, 2, ..., 9997, 9998, 9999])
132137
133138
"""
134-
may_need_closing = isinstance(store, str)
139+
may_need_closing = _might_close(store)
135140
store = normalize_store_arg(store, clobber=True)
136141
try:
137142
_create_array(arr, store=store, overwrite=True, **kwargs)
@@ -202,7 +207,7 @@ def save_group(store, *args, **kwargs):
202207
if len(args) == 0 and len(kwargs) == 0:
203208
raise ValueError('at least one array must be provided')
204209
# handle polymorphic store arg
205-
may_need_closing = isinstance(store, str)
210+
may_need_closing = _might_close(store)
206211
store = normalize_store_arg(store, clobber=True)
207212
try:
208213
grp = _create_group(store, overwrite=True)

zarr/creation.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
from pathlib import Path
13
from warnings import warn
24

35
import numpy as np
@@ -148,7 +150,9 @@ def create(shape, chunks=True, dtype=None, compressor='default',
148150
def normalize_store_arg(store, clobber=False, storage_options=None, mode='w'):
149151
if store is None:
150152
return dict()
151-
elif isinstance(store, str):
153+
if isinstance(store, Path):
154+
store = os.fspath(store)
155+
if isinstance(store, str):
152156
mode = mode if clobber else "r"
153157
if "://" in store or "::" in store:
154158
return FSStore(store, mode=mode, **(storage_options or {}))

zarr/storage.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import multiprocessing
2121
import operator
2222
import os
23+
from pathlib import Path
2324
import re
2425
import shutil
2526
import sys

0 commit comments

Comments
 (0)