10000 Add `move` method for `Group` · rjgildea/zarr-python@c695e9c · GitHub
[go: up one dir, main page]

Skip to content

Commit c695e9c

Browse files
committed
Add move method for Group
Leverages the `rename` function for `Store`s to apply renaming to the keys. The net effect is moving a `Group` and its contents or an `Array` and its chunks.
1 parent ff00eb6 commit c695e9c

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

zarr/hierarchy.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from zarr.attrs import Attributes
1111
from zarr.core import Array
1212
from zarr.storage import (contains_array, contains_group, init_group,
13-
DictStore, group_meta_key, attrs_key, listdir, rmdir)
13+
DictStore, group_meta_key, attrs_key, listdir, rename, rmdir)
1414
from zarr.creation import (array, create, empty, zeros, ones, full,
1515
empty_like, zeros_like, ones_like, full_like,
1616
normalize_store_arg)
@@ -81,6 +81,7 @@ class Group(MutableMapping):
8181
ones_like
8282
full_like
8383
info
84+
move
8485
8586
"""
8687

@@ -934,6 +935,37 @@ def _full_like_nosync(self, name, data, **kwargs):
934935
return full_like(data, store=self._store, path=path,
935936
chunk_store=self._chunk_store, **kwargs)
936937

938+
def _move_nosync(self, path, new_path):
939+
rename(self._store, path, new_path)
940+
if self._chunk_store is not None:
941+
rename(self._chunk_store, path, new_path)
942+
943+
def move(self, source, dest):
944+
"""Move contents from one path to another relative to the Group.
945+
946+
Parameters
947+
----------
948+
source : string
949+
Name or path to a Zarr object to move.
950+
dest : string
951+
New name or path of the Zarr object.
952+
"""
953+
954+
source = self._item_path(source)
955+
dest = self._item_path(dest)
956+
957+
# Check that source exists.
958+
if not (contains_array(self._store, source) or contains_group(self._store, source)):
959+
raise ValueError('The source, "%s", does not exist.' % source)
960+
if contains_array(self._store, dest) or contains_group(self._store, dest):
961+
raise ValueError('The dest, "%s", already exists.' % dest)
962+
963+
# Ensure groups needed for `dest` exist.
964+
if "/" in dest:
965+
self.require_group("/" + dest.rsplit("/", 1)[0])
966+
967+
self._write_op(self._move_nosync, source, dest)
968+
937969

938970
def _normalize_store_arg(store, clobber=False):
939971
return normalize_store_arg(store, clobber=clobber, default=DictStore)

0 commit comments

Comments
 (0)
9 0