Data I/O

This module provides input/output utilities for loading and saving concept data.

Summary

I/O Functions and Classes

extract_zip

Extract a zip archive to a specific folder.

extract_tar

Extract a tar (or tar.gz) archive to a specific folder.

save_pickle

Save object to file as pickle.

load_pickle

Load object from pickle file.

download_url

Downloads the content of an URL to a specific folder.

DownloadProgressBar

Progress bar for file downloads.

Function Documentation

extract_zip(path: str, folder: str)[source]

Extract a zip archive to a specific folder.

Parameters:
  • path – The path to the zip archive.

  • folder – The destination folder.

extract_tar(path: str, folder: str, verbose: bool = True)[source]

Extract a tar (or tar.gz) archive to a specific folder.

Parameters:
  • path – The path to the tar(gz) archive.

  • folder – The destination folder.

  • verbose – If False, will not show progress bars (default: True).

save_pickle(obj: Any, filename: str) str[source]

Save object to file as pickle.

Parameters:
  • obj – Object to be saved.

  • filename – Where to save the file.

Returns:

The absolute path to the saved pickle.

Return type:

str

load_pickle(filename: str) Any[source]

Load object from pickle file.

Parameters:

filename – The absolute path to the saved pickle.

Returns:

The loaded object.

Return type:

Any

download_url(url: str, folder: str, filename: str | None = None, verbose: bool = True)[source]

Downloads the content of an URL to a specific folder.

Parameters:
  • url (string) – The url.

  • folder (string) – The folder.

  • filename (string, optional) – The filename. If None, inferred from url.

  • verbose (bool, optional) – If False, will not show progress bars. (default: True)

Class Documentation

class DownloadProgressBar(*_, **__)[source]

Bases: tqdm

Progress bar for file downloads.

Extends tqdm to show download progress with file size information. Adapted from https://stackoverflow.com/a/53877507

update_to(b=1, bsize=1, tsize=None)[source]

Update progress bar based on download progress.

Parameters:
  • b – Number of blocks transferred so far (default: 1).

  • bsize – Size of each block in bytes (default: 1).

  • tsize – Total size in blocks (default: None).