Skip to main content

Wrap tempfile to give you pathlib.Path.

Project description

temppathlib

Check status Test coverage PyPI - version PyPI - Python Version

Temppathlib provides wrappers around tempfile so that you can directly use them together with pathlib module. We found it cumbersome to convert tempfile objects manually to pathlib.Path whenever we needed a temporary file.

Additionally, we also provide:

  • a context manager removing_tree that checks if a path exists and recursively deletes it by wrapping shutil.rmtree.

  • a context manager TmpDirIfNecessary that creates a temporary directory if no directory is given and otherwise uses a supplied directory. This is useful when you want to keep some of the temporary files for examination after the program finished. We usually specify an optional --operation_dir command-line argument to our programs and pass its value to the TmpDirIfNecessary.

If you need a more complex library to transition from string paths to pathlib.Path, have a look at ruamel.std.pathlib.

Usage

import pathlib

import temppathlib

# create a temporary directory
with temppathlib.TemporaryDirectory() as tmp_dir:
    tmp_pth = tmp_dir.path / "some-filename.txt"
    # do something else with tmp_dir ...

# create a temporary file
with temppathlib.NamedTemporaryFile() as tmp:
    # write to it
    tmp.file.write('hello'.encode())
    tmp.file.flush()

    # you can use its path.
    target_pth = pathlib.Path('/some/permanent/directory') / tmp.path.name

# create a temporary directory only if necessary
operation_dir = pathlib.Path("/some/operation/directory)
with temppathlib.TmpDirIfNecessary(path=operation_dir) as op_dir:
    # do something with the operation directory
    pth = op_dir.path / "some-file.txt"

    # operation_dir is not deleted since 'path' was specified.


with temppathlib.TmpDirIfNecessary() as op_dir:
    # do something with the operation directory
    pth = op_dir.path / "some-file.txt"

    # op_dir is deleted since 'path' argument was not specified.

# context manager to remove the path recursively
pth = pathlib.Path('/some/directory')
with temppathlib.removing_tree(pth):
    # do something in the directory ...
    pass

Installation

  • Create a virtual environment:

python3 -m venv venv3
  • Activate it:

source venv3/bin/activate
  • Install temppathlib with pip:

pip3 install temppathlib

Development

  • Check out the repository.

  • In the repository root, create the virtual environment:

python3 -m venv venv3
  • Activate the virtual environment:

source venv3/bin/activate
  • Install the development dependencies:

pip3 install -e .[dev]
  • We use tox for testing and packaging the distribution. Assuming that the virtual environment has been activated and the development dependencies have been installed, run:

tox
  • We also provide a set of pre-commit checks that lint and check code for formatting. Run them locally from an activated virtual environment with development dependencies:

./precommit.py
  • The pre-commit script can also automatically format the code:

./precommit.py  --overwrite

Versioning

We follow Semantic Versioning. The version X.Y.Z indicates:

  • X is the major version (backward-incompatible),

  • Y is the minor version (backward-compatible), and

  • Z is the patch version (backward-compatible bug fix).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

temppathlib-1.1.0.tar.gz (6.2 kB view details)

Uploaded Source

File details

Details for the file temppathlib-1.1.0.tar.gz.

File metadata

  • Download URL: temppathlib-1.1.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for temppathlib-1.1.0.tar.gz
Algorithm Hash digest
SHA256 b31f8bac63fee396eedda11b91ee43aaef4395d09f5f5b0cb9770254ef097d1f
MD5 6770b2c476e0dbc59d1338bc17ce7e73
BLAKE2b-256 b90c82b7812123cb0fecbacae7b077f5fa7623844262e7127525bb2d00b4f377

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page