Skip to main content

scratchdir

Build Status codecov Code Climate Issue Count

PyPI Version PyPI Versions

Context manager to maintain your temporary directories/files.

Installation

To install scratchdir from pip:

    $ pip install scratchdir

To install scratchdir from source:

    $ git clone git@github.com:ahawker/scratchdir.git
    $ python setup.py install

Usage

Creating a new ScratchDir is simple. Just instantiate a new instance and call setup:

⇒  cat examples/readme/setup.py
import scratchdir

sd = scratchdir.ScratchDir()
sd.setup()
print(sd.wd)
sd.teardown()

⇒  python examples/readme/usage-1.py
/var/folders/86/zhtx1pv53qs2mm1fq1k1841w0000gn/T/3e56r54m.scratchdir

Or as a context manager using the with statement:

⇒  cat examples/readme/context-manager.py
import scratchdir

with scratchdir.ScratchDir() as sd:
    print(sd.wd)

⇒  python examples/readme/context-manager.py
/var/folders/86/zhtx1pv53qs2mm1fq1k1841w0000gn/T/_ibfhq1s.scratchdir

Files created by the ScratchDir are automatically cleaned up on teardown:

⇒  cat examples/readme/cleanup.py
import os
import scratchdir

path = None

with scratchdir.ScratchDir() as sd:
    tmp = sd.named(delete=False)
    path = tmp.name
    print('Path {} exists? {}'.format(path, os.path.exists(path)))

print('Path {} exists? {}'.format(path, os.path.exists(path)))

⇒  python examples/readme/cleanup.py
Path /var/folders/86/zhtx1pv53qs2mm1fq1k1841w0000gn/T/y1aedyk8.scratchdir/tmp7m79rev1 exists? True
Path /var/folders/86/zhtx1pv53qs2mm1fq1k1841w0000gn/T/y1aedyk8.scratchdir/tmp7m79rev1 exists? False

Directories within the ScratchDir are also easy to create:

⇒  cat examples/readme/directory.py
import scratchdir

with scratchdir.ScratchDir() as sd:
    subdir = sd.directory()
    print(subdir)

⇒  python examples/readme/directory.py
/var/folders/86/zhtx1pv53qs2mm1fq1k1841w0000gn/T/c1odkxbw.scratchdir/tmpcyeqjk1v

Methods on the ScratchDir instance will pass arguments down to their corresponding functions in tempfile.

⇒  cat examples/readme/params.py
import scratchdir

with scratchdir.ScratchDir() as sd:
    tmp = sd.named(suffix='.txt', prefix='logfile-', delete=False)
    print(tmp.name)

⇒  python examples/readme/params.py
/var/folders/86/zhtx1pv53qs2mm1fq1k1841w0000gn/T/1h_7379t.scratchdir/logfile-z1gq195q.txt

Creating a hierarchy of ScratchDir instances to match that of your domain objects is also simple:

⇒  cat examples/readme/hierarchy.py
import scratchdir

with scratchdir.ScratchDir(prefix='grandparent-') as grandparent:
    print(grandparent.wd)
    with grandparent.child(prefix='parent-') as parent:
        print(parent.wd)
        with parent.child(prefix='child-') as child:
            print(child.wd)

⇒  python examples/readme/hierarchy.py
/var/folders/86/zhtx1pv53qs2mm1fq1k1841w0000gn/T/grandparent-4ld_pl8f.scratchdir
/var/folders/86/zhtx1pv53qs2mm1fq1k1841w0000gn/T/grandparent-4ld_pl8f.scratchdir/parent-s6y_gmxg.scratchdir
/var/folders/86/zhtx1pv53qs2mm1fq1k1841w0000gn/T/grandparent-4ld_pl8f.scratchdir/parent-s6y_gmxg.scratchdir/child-28k2hpdk.scratchdir

Methods

The ScratchDir instance maintains a set of bound methods are map to functions/classes within the tempfile module in the standard library. A table of methods is as follows:

scratchdir tempfile description
file, TemporaryFile TemporaryFile Create a nameless temporary file that is automatically deleted once it's closed.
named, NamedTemporaryFile NamedTemporaryFile Create a temporary file that receives a filename on disk that is automatically deleted once it's closed unless the delete parameter is False.
spooled, SpooledTemporaryFile SpooledTemporaryFile Create a temporary file that will overflow from memory onto disk once a defined maximum size is exceeded.
secure, mkstemp mkstemp Create a temporary file in as secure way as possible.
directory, mkdtemp mkdtemp Create a temporary directory.
filename N/A Create a unique filename within the ScratchDir.
join N/A Join a number of paths to the root of the ScratchDir.

Goals

I've implemented similar functionality three times now, starting with my scatter project back in 2013-2014. I'd rather not write it again, so the goal is that scratchdir should be generic and reusable for future projects.

Contributing

If you would like to contribute, simply fork the repository, push your changes and send a pull request.

License

Scratchdir is avaialble under the Apache 2.0 license.

Metadata

Release files for scratchdir 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for scratchdir 1.0.2
File Size Uploaded
scratchdir-1.0.2.tar.gz 10.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for scratchdir 1.0.2
File Interpreter ABI Platform
scratchdir-1.0.2-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 20.4 kB

Release files / scratchdir-1.0.2.tar.gz

Download URL scratchdir-1.0.2.tar.gz
Size 10.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5c1658f3bf69f4fd6e10183a4529311d118804db44e6c0fcf75b5e1f992053c0
BLAKE2b-256 checksum
How to use checksums
6e74c99545b3eec4f0c0e8e12f72679d58648402e5ae007adec1ed16874ddb06
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.0

Release files / scratchdir-1.0.2-py2.py3-none-any.whl

Download URL scratchdir-1.0.2-py2.py3-none-any.whl
Size 10.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
a7ab18859d3f56fe9b7c971995653ed8cd2bf29795ebd794e70930f6adb831ff
BLAKE2b-256 checksum
How to use checksums
1823c28e20c27c0c46549890187cd7eef1bf719d1d0a417bd1505abcd58e2dbd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.0

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page