[docs]classReloadedArraySeed(WrapperArraySeed):""" Seed for the :py:class:`~ReloadedArray` class. This is a subclass of :py:class:`~dolomite_matrix.WrapperArraySeed.WrapperArraySeed`. """def__init__(self,seed,path:str):""" Args: seed: The contents of the reloaded array. path: Path to the directory containing the on-disk representation. """super(ReloadedArraySeed,self).__init__(seed)self._path=path@propertydefpath(self)->str:""" Returns: Path to the directory containing the on-disk representation. """returnself._path
[docs]classReloadedArray(delayedarray.DelayedArray):""" An array that was reloaded from disk by the :py:func:`~dolomite_base.read_object.read_object` function, and remembers the path from which it was loaded. This class allows methods to refer to the existing on-disk representation by inspecting the path. For example, :py:func:`~dolomite_base.save_object.save_object` can just copy/link to the existing files instead of repeating the saving process. """def__init__(self,seed,path:str):""" To construct a ``ReloadedArray`` from an existing :py:class:`~ReloadedArraySeed`, use :py:meth:`~delayedarray.wrap.wrap` instead. Args: seed: The contents of the reloaded array. path: Path to the directory containing the on-disk representation. """ifnotisinstance(seed,ReloadedArraySeed):seed=ReloadedArraySeed(seed,path)super(ReloadedArray,self).__init__(seed)@propertydefpath(self)->str:""" Returns: Path to the directory containing the on-disk representation. """returnself.seed._path
[docs]@save_object.registerdefsave_object_ReloadedArray(x:ReloadedArray,path:str,reloaded_array_reuse_mode:str="link",**kwargs):""" Method for saving :py:class:`~ReloadedArray.ReloadedArray` objects to disk, see :py:meth:`~dolomite_base.save_object.save_object` for details. Args: x: Object to be saved. path: Path to a directory to save ``x``. reloaded_array_reuse_mode: How the files in ``x.path`` should be re-used when populating ``path``. This can be ``"link"``, to create a hard link to each file; ``"symlink"``, to create a symbolic link to each file; ``"copy"``, to create a copy of each file; or ``"none"``, to perform a fresh save of ``x`` without relying on ``x.path``. kwargs: Further arguments, ignored. Returns: ``x`` is saved to ``path``. """ifreloaded_array_reuse_mode=="none":ifdelayedarray.is_sparse(x):return_save_compressed_sparse_matrix(x,path,**kwargs)else:return_save_dense_array(x,path,**kwargs)ifreloaded_array_reuse_mode=="link":defFUN(src,dest):try:os.link(src,dest)exceptExceptionas_:shutil.copyfile(src,dest)elifreloaded_array_reuse_mode=="symlink":defFUN(src,dest):try:os.symlink(src,dest)exceptExceptionas_:shutil.copyfile(src,dest)elifreloaded_array_reuse_mode=="copy":FUN=shutil.copyfileelse:raiseValueError("invalid reuse mode '"+reloaded_array_reuse_mode+"'")forroot,dirs,filesinos.walk(x.path):newpath=os.path.join(path,os.path.relpath(root,x.path))os.makedirs(newpath)forfinfiles:FUN(os.path.join(root,f),os.path.join(newpath,f))