Package: cachepath
A small package for pythonic parameterized cache paths.Getting StartedInstall:pip install cachepathImport:from cachepath import CachePath, TempPath, PathDocs:ReadTheDocs|API doc is hereWhy?Integratespathlibwithtempfile.gettempdirandshutil.rmtreeby providingTempPathandPath.rm():path = TempPath()
path.rm()
# or would you rather..
path = None
with tempfile.NamedTemporaryFile(delete=False) as f:
    path = Path(f.name)
# Only now can we use Path. If we tried using it within the With
# (for example for path.read_text()), we'd break on Windows
path.unlink()  # only if file, doesn't work on foldersWrapspathlibimport for Py2/3 compat. (not insix!):from cachepath import Path
# or
try: from pathlib import Path; except ImportError: from pathlib2 import PathProvidesCachePath, which lets you quickly get a parameterized temp filename, with all folders automatically created:r = CachePath(date, userid, 'expensive_results.txt')
assert (r == Path('/tmp/', date, userid, 'expensive_results.txt')
        and r.parent.exists())
r.rm()  # File remove
r.parent.rm()  # Symmetric with folder remove!

# Without cachepath
p = Path(tempfile.gettempdir(), date, userid, 'expensive_results.txt').
# Don't update timestamp if it already exists so that we don't cause
# Make-like tools to always think something's changed
if not p.parent.exists():
    p.parent.mkdir(parents=True, exist_ok=True)

p.unlink()  # Why is it .unlink() instead of .remove()?
# Why .remove and .unlink, but mkdir instead of createdir?
p.parent.remove()
# .remove() might throw because there was another file in the folder,
# but we didn't care, they're tempfiles!
import shutil
shutil.rmtree(p.parent)Why, but longer:Do you need a temp path to pass to some random tool for its logfile?
Behold, a gaping hole inpathlib:import tempfile
import os
try: from pathlib import Path; except ImportError: from pathlib2 import Path
def get_tempfile():
    fd, loc = tempfile.mkstemp()
    os.close(fd)  # If we forgot do this, it would stay open until process exit
    return Path(loc)

# Easier way
from cachepath import TempPath
def get_tempfile():
    return TempPath()  # Path('/tmp/213kjdsrandom')But this module is called cachepath, not temppath, what gives?Suppose I’m running that same imaginary tool pretty often, but I’d like to skip running
it if I already have results for a certain day. Just sticking some identifying info into a filename
should be good enough.
Something likePath('/tmp/20181204_toolresults.txt')# try: from pathlib import Path; except ImportError: from pathlib2 import Path
# We'll cheat a little to get py2/3 compat without so much ugliness
from cachepath import Path
import tempfile
def get_tempfile(date):
    filename = '{}_toolresults.txt'.format(date)
    return Path(tempfile.gettempdir(), filename)

# Easier to do this...
from cachepath import CachePath
def get_tempfile(date):
    return CachePath(date, suffix='.txt')Not bad, but not great. But our requirements changed, let’s go a step further.Now I’m running this toola lot, over a tree of data that looks
like this:2018-12-23
    person1
    person2
2018-12-24
    person1
2018-12-25
    person1I want my logs to be structured the same way.  How hard can it be?2018-12-23/
    person1_output.txt
    person2_output.txt
2018-12-24/
    person1_output.txt
2018-12-25/
    person1_output.txtLet’s find out:# Let's get the easy way out of the way first :)
def get_path(date, person):
    return CachePath(date, person, suffix='_output.txt')
    # Automatically ensures /tmp/date/ exists when we create the CachePath!

# Now the hard way
def get_path(date, person):
    personfilename = '{p}_output.txt'.format(p=person)
    returning = Path(tempfile.gettempdir())/date/personfilename
    # Does this mkdir update the modified timestamp of the folders we're in?
    # Might matter if we're part of a larger toolset...
    returning.parent.mkdir(exist_ok=True, parents=True)
    return returningSuppose we hadn’t remembered to make the$date/folders. When we passed the
Path out to another tool, or tried to .open it,
we may have gotten a Permission Denied
error on Unix systems rather than the “File/Folder not found” you might expect.
With CachePath, this can’t happen. Creating a CachePath implicitly creates all
of the preceding directories necessary for your file to exist.Now, suppose we found a bug in this external tool we were using and we’re going
to re-run it for a day.
How do we clear out that day’s results so that we can be sure we’re looking
at fresh output from the tool? Well, with CachePath, it’s just:def easy_clear_date(date):
    CachePath(date).clear()  # rm -r /tmp/date/*But if you don’t have cachepath, you’ll find that most Python libs play it
pretty safe when it comes to files. Path.remove() requires the folder to be empty,
and doesn’t provide a way to empty the folder. Not to mention, what if our results
folders had special permissions, or was actually a symlink, and we had write access
but not delete? Oh well,
let’s see what we can do:def hard_clear_date(date):
    # We happen to know that date is a folder and not a file (at least in our
    # current design), so we know we need some form of .remove() rather than
    # .unlink(). Unfortunately, pathlib doesn't offer one for folders with
    # files still in them. If you google how to do it, you will find plenty of
    # answers, one of which is a pure pathlib recursive solution! But we're lazy,
    # so lets bring in yet another module:
    p = Path(tempfile.gettempdir(), date)
    import shutil
    if p.exists():
        shutil.rmtree(p)
    p.mkdir(exist_ok=True, parents=True)
    # This still isn't exactly equivalent to CachePath.clear(), because we've
    # lost whatever permissions were set on the date folder, and if it were
    # actually a symlink to somewhere else, that's gone now.Convinced yet?pip install cachepathor copythe sourceinto your localutils.py(you know you have one.)API doc is here.By the way, as a side effect of importingcachepath, all Paths get the ability
to dorm()andclear().Shameless PromoFind yourself working with paths a lot in cmd-line tools? You might likeinvokeand/ormagicinvoke![*]The source for CachePath can be downloaded from theGithub repo.[†]This package was created withCookiecutterand theaudreyr/cookiecutter-pypackageproject template.History1.0.0 (2018-12-08)Big doc updates. 1.0.0 to symbolize SemVer adherence.0.1.0 (2018-12-08)First release on PyPI. Adds CachePath, TempPath, Path.