From f4829d008ad09a1c70c171de645caf3ac70b0331 Mon Sep 17 00:00:00 2001 From: ljgray Date: Mon, 13 Mar 2023 18:50:45 -0700 Subject: [PATCH] feat(memh5): add '.copy' method to MemDiskGroup Closes #190 --- caput/memh5.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/caput/memh5.py b/caput/memh5.py index ff61149b..422c1ad1 100644 --- a/caput/memh5.py +++ b/caput/memh5.py @@ -368,8 +368,7 @@ def require_group(self, name): class MemGroup(_BaseGroup): - """ - In memory implementation of the :class:`h5py.Group`. + """In memory implementation of the :class:`h5py.Group`. This class doubles as the memory implementation of :class:`h5py.File`, object, since the distinction between a file and a group for in-memory data @@ -2007,6 +2006,30 @@ def flush(self): if self.ondisk: self._data.flush() + def copy(self, shared: list = []) -> MemDiskGroup: + """Return a deep copy of this class or subclass. + + Parameters + ---------- + shared + dataset names to share (i.e. don't deep copy) + + Returns + ------- + copy + Copy of this group. Datasets in shared are shallow copies, + everything else is a deep copy + """ + cls = self.__class__.__new__(self.__class__) + MemDiskGroup.__init__(cls, distributed=self.distributed, comm=self.comm) + deep_group_copy(self._data, cls._data, deep_copy_dsets=True, shared=shared) + + return cls + + def __deepcopy__(self, memo, /) -> MemDiskGroup: + """Called when copy.deepcopy is called on this class""" + return self.copy() + def save( self, filename,