def __init__(self, store, path=None, read_only=False, chunk_store=None, cache_attrs=True, synchronizer=None): self._store = store self._chunk_store = chunk_store self._path = normalize_storage_path(path) if self._path: self._key_prefix = self._path + '/' else: self._key_prefix = '' self._read_only = read_only self._synchronizer = synchronizer # guard conditions if contains_array(store, path=self._path): err_contains_array(path) # initialize metadata try: mkey = self._key_prefix + group_meta_key meta_bytes = store[mkey] except KeyError: err_group_not_found(path) else: meta = decode_group_metadata(meta_bytes) self._meta = meta # setup attributes akey = self._key_prefix + attrs_key self._attrs = Attributes(store, key=akey, read_only=read_only, cache=cache_attrs, synchronizer=synchronizer) # setup info self._info = InfoReporter(self)
def open_group(store, mode='a', cache_attrs=True, synchronizer=None, path=None): """Open a group using file-mode-like semantics. Parameters ---------- store : MutableMapping or string Store or path to directory in file system or name of zip file. mode : {'r', 'r+', 'a', 'w', 'w-'}, optional Persistence mode: 'r' means read only (must exist); 'r+' means read/write (must exist); 'a' means read/write (create if doesn't exist); 'w' means create (overwrite if exists); 'w-' means create (fail if exists). cache_attrs : bool, optional If True (default), user attributes will be cached for attribute read operations. If False, user attributes are reloaded from the store prior to all attribute read operations. synchronizer : object, optional Array synchronizer. path : string, optional Group path within store. Returns ------- g : zarr.hierarchy.Group Examples -------- >>> import zarr >>> root = zarr.open_group('data/example.zarr', mode='w') >>> foo = root.create_group('foo') >>> bar = root.create_group('bar') >>> root <zarr.hierarchy.Group '/'> >>> root2 = zarr.open_group('data/example.zarr', mode='a') >>> root2 <zarr.hierarchy.Group '/'> >>> root == root2 True """ # handle polymorphic store arg store = _normalize_store_arg(store) path = normalize_storage_path(path) # ensure store is initialized if mode in ['r', 'r+']: if contains_array(store, path=path): err_contains_array(path) elif not contains_group(store, path=path): err_group_not_found(path) elif mode == 'w': init_group(store, overwrite=True, path=path) elif mode == 'a': if contains_array(store, path=path): err_contains_array(path) if not contains_group(store, path=path): init_group(store, path=path) elif mode in ['w-', 'x']: if contains_array(store, path=path): err_contains_array(path) elif contains_group(store, path=path): err_contains_group(path) else: init_group(store, path=path) # determine read only status read_only = mode == 'r' return Group(store, read_only=read_only, cache_attrs=cache_attrs, synchronizer=synchronizer, path=path)
def open_group(store=None, mode='a', synchronizer=None, path=None): """Open a group using mode-like semantics. Parameters ---------- store : MutableMapping or string Store or path to directory in file system. mode : {'r', 'r+', 'a', 'w', 'w-'} Persistence mode: 'r' means read only (must exist); 'r+' means read/write (must exist); 'a' means read/write (create if doesn't exist); 'w' means create (overwrite if exists); 'w-' means create (fail if exists). synchronizer : object, optional Array synchronizer. path : string, optional Group path. Returns ------- g : zarr.hierarchy.Group Examples -------- >>> import zarr >>> root = zarr.open_group('example', mode='w') >>> foo = root.create_group('foo') >>> bar = root.create_group('bar') >>> root Group(/, 2) groups: 2; bar, foo store: DirectoryStore >>> root2 = zarr.open_group('example', mode='a') >>> root2 Group(/, 2) groups: 2; bar, foo store: DirectoryStore >>> root == root2 True """ # handle polymorphic store arg store = _handle_store_arg(store) path = normalize_storage_path(path) # ensure store is initialized if mode in ['r', 'r+']: if contains_array(store, path=path): err_contains_array(path) elif not contains_group(store, path=path): err_group_not_found(path) elif mode == 'w': init_group(store, overwrite=True, path=path) elif mode == 'a': if contains_array(store, path=path): err_contains_array(path) if not contains_group(store, path=path): init_group(store, path=path) elif mode in ['w-', 'x']: if contains_array(store, path=path): err_contains_array(path) elif contains_group(store, path=path): err_contains_group(path) else: init_group(store, path=path) # determine read only status read_only = mode == 'r' return Group(store, read_only=read_only, synchronizer=synchronizer, path=path)