Project: cachepath

Pythonic parameterized cache paths.

Project Details

Latest version
1.1.1
Home Page
https://github.com/haydenflinner/cachepath
PyPI Page
https://pypi.org/project/cachepath/

Project Popularity

PageRank
0.0019055198234403962
Number of downloads
51904

Welcome to cachepath's documentation!

.. image:: https://img.shields.io/pypi/v/cachepath.svg :target: https://pypi.python.org/pypi/cachepath

.. image:: https://img.shields.io/travis/haydenflinner/cachepath.svg :target: https://travis-ci.org/haydenflinner/cachepath

.. image:: https://readthedocs.org/projects/cachepath/badge/?version=latest :target: https://cachepath.readthedocs.io/en/latest/?badge=latest :alt: Documentation Status

A small package for pythonic parameterized cache paths.

Getting Started

Install: pip install cachepath

Import: from cachepath import CachePath, TempPath, Path

Docs: ReadTheDocs_ | API doc is here_

Why? 1. Integrates pathlib with tempfile.gettempdir and shutil.rmtree by providing TempPath and Path.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 folders

2. Wraps ``pathlib`` import for Py2/3 compat. (not in ``six``!)::

      from cachepath import Path
      # or
      try: from pathlib import Path; except ImportError: from pathlib2 import Path

3. Provides `CachePath`_, 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 in pathlib::

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 like Path('/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 tool a lot, over a tree of data that looks like this::

2018-12-23
    person1
    person2
2018-12-24
    person1
2018-12-25
    person1

I 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.txt

Let'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 returning

Suppose 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 cachepath or copy the source_ into your local utils.py (you know you have one.)

API doc is here_.

By the way, as a side effect of importing cachepath, all Paths get the ability to do rm() and clear().

Shameless Promo

Find yourself working with paths a lot in cmd-line tools? You might like invoke_ and/or magicinvoke_!

.. [*] The source for CachePath can be downloaded from the Github repo_.

.. Github repo: https://github.com/haydenflinner/cachepath .. [*] This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage_ project template.

.. _the source: https://github.com/haydenflinner/cachepath/blob/master/cachepath/init.py .. _Cookiecutter: https://github.com/audreyr/cookiecutter .. _audreyr/cookiecutter-pypackage: https://github.com/audreyr/cookiecutter-pypackage .. _invoke: https://www.pyinvoke.org .. _magicinvoke: https://magicinvoke.readthedocs.io/en/latest/ .. _ReadTheDocs: https://cachepath.readthedocs.io/en/latest/ .. _API doc is here: https://cachepath.readthedocs.io/en/latest/cachepath.html .. _CachePath: https://cachepath.readthedocs.io/en/latest/cachepath.html#cachepath.CachePath

======= History

1.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.