Skip to content

capturegraph.data.save.copy #

Copy - Copy files using zip broadcasting#

Copy files from a List of source paths to destination paths.

Usage
import capturegraph.data as cg

# Build destination paths with zip
dst = cg.zip(dir=Path("./output"), name=names).map(lambda r: r.dir / r.name)

# Copy files
cg.copy(src=sessions.photo, dst=dst)

copy(*, src, dst) #

Copy files from source paths to destination paths.

Uses zip internally to pair sources and destinations.

Parameters:

Name Type Description Default
src List[Path]

List of source file paths

required
dst List[Path | str]

List of destination paths (same length as src)

required

Returns:

Type Description
List[Path]

List of destination paths that were created

Example
import capturegraph.data as cg

# Build destination paths
names = sessions.date.map(lambda d: f"{d:%Y%m%d}.heic")
dst = cg.zip(dir=Path("./output"), name=names).map(lambda r: r.dir / r.name)

# Copy files
copied = cg.copy(src=sessions.photo, dst=dst)
Source code in capturegraph-lib/capturegraph/data/save/copy.py
def copy(*, src: List[Path], dst: List[Path | str]) -> List[Path]:
    """Copy files from source paths to destination paths.

    Uses zip internally to pair sources and destinations.

    Args:
        src: List of source file paths
        dst: List of destination paths (same length as src)

    Returns:
        List of destination paths that were created

    Example:
        ```python
        import capturegraph.data as cg

        # Build destination paths
        names = sessions.date.map(lambda d: f"{d:%Y%m%d}.heic")
        dst = cg.zip(dir=Path("./output"), name=names).map(lambda r: r.dir / r.name)

        # Copy files
        copied = cg.copy(src=sessions.photo, dst=dst)
        ```
    """

    def _copy_one(r) -> Path:
        src_path = r.src
        dst_path = Path(r.dst)

        if is_missing(src_path) or src_path is None:
            return MissingType(ValueError(f"Source path is missing: {src_path}"))

        dst_path.parent.mkdir(parents=True, exist_ok=True)
        copy2(src_path, dst_path)
        return dst_path

    return zip(src=src, dst=dst) @ _copy_one