@@ -32,9 +32,8 @@ def create(shape, chunks=None, dtype=None, compressor='default',
3232 Default value to use for uninitialized portions of the array.
3333 order : {'C', 'F'}, optional
3434 Memory layout to be used within each chunk.
35- store : MutableMapping, optional
36- Array storage. If not provided, a Python dict will be used, meaning
37- array data will be stored in memory.
35+ store : MutableMapping or string
36+ Store or path to directory in file system.
3837 synchronizer : object, optional
3938 Array synchronizer.
4039 overwrite : bool, optional
@@ -72,9 +71,8 @@ def create(shape, chunks=None, dtype=None, compressor='default',
7271
7372 """ # flake8: noqa
7473
75- # initialize store
76- if store is None :
77- store = dict ()
74+ # handle polymorphic store arg
75+ store = _handle_store_arg (store )
7876
7977 # compatibility
8078 compressor , fill_value = _handle_kwargs (compressor , fill_value , kwargs )
@@ -92,6 +90,15 @@ def create(shape, chunks=None, dtype=None, compressor='default',
9290 return z
9391
9492
93+ def _handle_store_arg (store ):
94+ if store is None :
95+ return dict ()
96+ elif isinstance (store , str ):
97+ return DirectoryStore (store )
98+ else :
99+ return store
100+
101+
95102def _handle_kwargs (compressor , fill_value , kwargs ):
96103
97104 # to be compatible with h5py, as well as backwards-compatible with Zarr
@@ -280,16 +287,16 @@ def array(data, **kwargs):
280287 return z
281288
282289
283- def open_array (path , mode = 'a' , shape = None , chunks = None , dtype = None ,
290+ def open_array (store = None , mode = 'a' , shape = None , chunks = None , dtype = None ,
284291 compressor = 'default' , fill_value = None , order = 'C' ,
285- synchronizer = None , filters = None , cache_metadata = True , ** kwargs ):
286- """Convenience function to instantiate an array stored in a
287- directory on the file system .
292+ synchronizer = None , filters = None , cache_metadata = True ,
293+ path = None , ** kwargs ):
294+ """Open array using mode-like semantics .
288295
289296 Parameters
290297 ----------
291- path : string
292- Path to directory in file system in which to store the array .
298+ store : MutableMapping or string
299+ Store or path to directory in file system.
293300 mode : {'r', 'r+', 'a', 'w', 'w-'}
294301 Persistence mode: 'r' means read only (must exist); 'r+' means
295302 read/write (must exist); 'a' means read/write (create if doesn't
@@ -316,6 +323,8 @@ def open_array(path, mode='a', shape=None, chunks=None, dtype=None,
316323 lifetime of the object. If False, array metadata will be reloaded
317324 prior to all data access and modification operations (may incur
318325 overhead depending on storage and data access pattern).
326+ path : string, optional
327+ Array path.
319328
320329 Returns
321330 -------
@@ -349,57 +358,56 @@ def open_array(path, mode='a', shape=None, chunks=None, dtype=None,
349358
350359 """ # flake8: noqa
351360
352- # use same mode semantics as h5py, although N.B., here `path` is a
353- # directory:
361+ # use same mode semantics as h5py
354362 # r : read only, must exist
355363 # r+ : read/write, must exist
356364 # w : create, delete if exists
357365 # w- or x : create, fail if exists
358366 # a : read/write if exists, create otherwise (default)
359367
360- # setup store
361- store = DirectoryStore ( path )
368+ # handle polymorphic store arg
369+ store = _handle_store_arg ( store )
362370
363371 # compatibility
364372 compressor , fill_value = _handle_kwargs (compressor , fill_value , kwargs )
365373
366374 # ensure store is initialized
367375
368376 if mode in ['r' , 'r+' ]:
369- if contains_group (store ):
377+ if contains_group (store , path = path ):
370378 raise ValueError ('store contains group' )
371- elif not contains_array (store ):
379+ elif not contains_array (store , path = path ):
372380 raise ValueError ('array does not exist' )
373381
374382 elif mode == 'w' :
375383 init_array (store , shape = shape , chunks = chunks , dtype = dtype ,
376384 compressor = compressor , fill_value = fill_value ,
377- order = order , filters = filters , overwrite = True )
385+ order = order , filters = filters , overwrite = True , path = path )
378386
379387 elif mode == 'a' :
380- if contains_group (store ):
388+ if contains_group (store , path = path ):
381389 raise ValueError ('store contains group' )
382- elif not contains_array (store ):
390+ elif not contains_array (store , path = path ):
383391 init_array (store , shape = shape , chunks = chunks , dtype = dtype ,
384392 compressor = compressor , fill_value = fill_value ,
385- order = order , filters = filters )
393+ order = order , filters = filters , path = path )
386394
387395 elif mode in ['w-' , 'x' ]:
388- if contains_group (store ):
396+ if contains_group (store , path = path ):
389397 raise ValueError ('store contains group' )
390- elif contains_array (store ):
398+ elif contains_array (store , path = path ):
391399 raise ValueError ('store contains array' )
392400 else :
393401 init_array (store , shape = shape , chunks = chunks , dtype = dtype ,
394402 compressor = compressor , fill_value = fill_value ,
395- order = order , filters = filters )
403+ order = order , filters = filters , path = path )
396404
397405 # determine read only status
398406 read_only = mode == 'r'
399407
400408 # instantiate array
401409 z = Array (store , read_only = read_only , synchronizer = synchronizer ,
402- cache_metadata = cache_metadata )
410+ cache_metadata = cache_metadata , path = path )
403411
404412 return z
405413
0 commit comments