Context manager to maintain your temporary directories/files.
To install scratchdir from pip:
$ pip install scratchdirTo install scratchdir from source:
$ git clone git@github.com:ahawker/scratchdir.git
$ python setup.py installCreating 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.scratchdirOr 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.scratchdirFiles 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/tmpcyeqjk1vMethods 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.txtCreating 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.scratchdirThe 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:
| scra tchd ir | temp file | desc ript ion |
|---|---|---|
| file , Temp orar yFil e | Temp orar yFil e | Crea te a name less temp orar y file that is auto mati call y dele ted once it's clos ed. |
| name d, Name dTem pora ryFi le | Name dTem pora ryFi le | Crea te a temp orar y file that rece ives a file name on disk that is auto mati call y dele ted once it's clos ed unle ss the ``de lete `` para mete r is ``Fa lse` `. |
| spoo led, Spoo ledT empo rary File | Spoo ledT empo rary File | Crea te a temp orar y file that will over flow from memo ry onto disk once a defi ned maxi mum size is exce eded . |
| secu re, mkst emp | mkst emp | Crea te a temp orar y file in as secu re way as poss ible . |
| dire ctor y, mkdt emp | mkdt emp | Crea te a temp orar y dire ctor y. |
| file name | N/A | Crea te a uniq ue file name with in the ``Sc ratc hDir ``. |
| join | N/A | Join a numb er of path s to the root of the ``Sc ratc hDir ``. |
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.
If you would like to contribute, simply fork the repository, push your changes and send a pull request.
Scratchdir is avaialble under the Apache 2.0 license.