fastdev.io

Submodules

Package Contents

fastdev.io.extract_archive(filename: str, extract_dir: str, remove_top_dir: bool = False)[source]

Extract archive file to a directory.

Currently only supports zip files.

Parameters:
  • filename (str) – Local path to the archive file.

  • extract_dir (str) – Directory to extract the archive file.

  • remove_top_dir (bool, optional) – Whether to remove the top-level directory in the archive. If True, the top-level common prefix directory will be removed. Defaults to False.

fastdev.io.cached_local_path(url: str, rel_cache_path: str | None = None, cache_root: str = FDEV_CACHE_ROOT) str[source]

Get the cached local path of the URL.

Parameters:
  • url (str) – Remote URL.

  • rel_cache_path (str, optional) – Relative local path in the cache root. Use the URL relative path if None. Defaults to None.

  • cache_root (str, optional) – Cache root. Defaults to FDEV_CACHE_ROOT.

Return type:

str

fastdev.io.download_url(url: str, local_path: str, verify: bool = True, force_redownload: bool = False) str[source]

Download url to local path.

Parameters:
  • url (str) – URL to download.

  • local_path (str) – Local path to save the downloaded file. If the local path is a directory, the file will be saved to the directory with the same name as the URL basename.

  • verify (bool, optional) – Verify SSL certificate. Defaults to True.

  • force_redownload (bool, optional) – Whether to force redownload the file even if it exists. Defaults to False.

Returns:

Local path of the downloaded file.

Return type:

str

fastdev.io.FILE_EXTENSIONS: typing_extensions.TypeAlias[source]
fastdev.io.dump(obj: Any, file: str | pathlib.Path | IO, file_format: FILE_EXTENSIONS | None = None, **kwargs)[source]

Dump obj to json/yaml file.

Parameters:
  • obj (Any) – Python object to dump.

  • file (Union[str, Path, IO]) – File path or IO object.

  • file_format (Optional[FILE_EXTENSIONS], optional) – File format. Defaults to None.

Examples

>>> import tempfile
>>> with tempfile.TemporaryDirectory() as tmpdir:
...     dump({"hello": "world"}, f"{tmpdir}/example.json")
fastdev.io.load(file: str | pathlib.Path | IO, file_format: FILE_EXTENSIONS | None = None, **kwargs) Any[source]

Load json/yaml/pickle file from file.

Parameters:
  • file (Union[str, Path, IO]) – file path or io object.

  • file_format (Optional[FILE_EXTENSIONS], optional) – file extension. Defaults to None.

Returns:

The loaded python object.

Return type:

Any

Examples

>>> load("assets/example.json")
{'hello': 'world'}