Skip to content

file

File Utility API

This module provides utility functions to interact with files.

Functions:

Functions

download_file

download_file(src: str, dst: os.PathLike, progress: bool = True, chunk_size: int = 8192)

Download file from supplied url to destination streaming.

Parameters:

  • src (str) –

    Source URL path

  • dst (PathLike) –

    Destination file path

  • progress (bool, default: True ) –

    Display progress bar. Defaults to True.

Source code in neuralspot_edge/utils/file.py
def download_file(src: str, dst: os.PathLike, progress: bool = True, chunk_size: int = 8192):
    """Download file from supplied url to destination streaming.

    Args:
        src (str): Source URL path
        dst (PathLike): Destination file path
        progress (bool, optional): Display progress bar. Defaults to True.

    """
    with requests.get(src, stream=True, timeout=3600 * 24) as r:
        r.raise_for_status()
        req_len = int(r.headers.get("Content-length", 0))
        prog_bar = tqdm(total=req_len, unit="iB", unit_scale=True) if progress else None
        with open(dst, "wb") as f:
            for chunk in r.iter_content(chunk_size=chunk_size):
                f.write(chunk)
                if prog_bar:
                    prog_bar.update(len(chunk))

compute_checksum

compute_checksum(file: Path, checksum_type: str = 'md5', chunk_size: int = 8192) -> str

Compute checksum of file.

Parameters:

  • file (Path) –

    File path

  • checksum_type (str, default: 'md5' ) –

    Checksum type. Defaults to "md5".

  • chunk_size (int, default: 8192 ) –

    Chunk size. Defaults to 8192.

Returns:

  • str ( str ) –

    Checksum value

Source code in neuralspot_edge/utils/file.py
def compute_checksum(file: Path, checksum_type: str = "md5", chunk_size: int = 8192) -> str:
    """Compute checksum of file.

    Args:
        file (Path): File path
        checksum_type (str, optional): Checksum type. Defaults to "md5".
        chunk_size (int, optional): Chunk size. Defaults to 8192.

    Returns:
        str: Checksum value
    """
    if not file.is_file():
        raise FileNotFoundError(f"File {file} not found.")
    hash_algo = hashlib.new(checksum_type)
    with open(file, "rb") as f:
        for chunk in iter(lambda: f.read(chunk_size), b""):
            hash_algo.update(chunk)
    return hash_algo.hexdigest()

load_pkl

load_pkl(file: str, compress: bool = True) -> dict[str, Any]

Load pickled file.

Parameters:

  • file (str) –

    File path (.pkl)

  • compress (bool, default: True ) –

    If file is compressed. Defaults to True.

Returns:

  • dict[str, Any]

    dict[str, Any]: Dictionary of pickled objects

Source code in neuralspot_edge/utils/file.py
def load_pkl(file: str, compress: bool = True) -> dict[str, Any]:
    """Load pickled file.

    Args:
        file (str): File path (.pkl)
        compress (bool, optional): If file is compressed. Defaults to True.

    Returns:
        dict[str, Any]: Dictionary of pickled objects
    """
    if compress:
        with gzip.open(file, "rb") as fh:
            return pickle.load(fh)
    else:
        with open(file, "rb") as fh:
            return pickle.load(fh)

save_pkl

save_pkl(file: str, compress: bool = True, **kwargs)

Save python objects into pickle file.

Parameters:

  • file (str) –

    File path (.pkl)

  • compress (bool, default: True ) –

    Whether to compress file. Defaults to True.

Source code in neuralspot_edge/utils/file.py
def save_pkl(file: str, compress: bool = True, **kwargs):
    """Save python objects into pickle file.

    Args:
        file (str): File path (.pkl)
        compress (bool, optional): Whether to compress file. Defaults to True.
    """
    if compress:
        with gzip.open(file, "wb") as fh:
            pickle.dump(kwargs, fh, protocol=4)
    else:
        with open(file, "wb") as fh:
            pickle.dump(kwargs, fh, protocol=4)

resolve_template_path

resolve_template_path(fpath: Path, **kwargs: Any) -> Path

Resolve templated path w/ supplied substitutions.

Parameters:

  • fpath (Path) –

    File path

  • **kwargs (Any, default: {} ) –

    Template arguments

Returns:

  • Path ( Path ) –

    Resolved file path

Source code in neuralspot_edge/utils/file.py
def resolve_template_path(fpath: Path, **kwargs: Any) -> Path:
    """Resolve templated path w/ supplied substitutions.

    Args:
        fpath (Path): File path
        **kwargs (Any): Template arguments

    Returns:
        Path: Resolved file path
    """
    return Path(Template(str(fpath)).safe_substitute(**kwargs))