Skip to main content

Pythonic parameterized cache paths.

Project description

https://img.shields.io/pypi/v/cachepath.svg https://img.shields.io/travis/haydenflinner/cachepath.svg Documentation Status

A small package for pythonic parameterized cache paths.

Getting Started

Install: pip install cachepath

Import: from cachepath import CachePath, Path

Docs: https://cachepath.readthedocs.io

Why?
  1. Integrates pathlib with tempfile and shutil

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

Why, but longer:

Do you need a temp path to pass to some random tool for its logfile? Behold, a gaping hole in pathlib:

def easy_get_tempfile():
    return TempPath()  # Path('/tmp/213kjdsrandom')
def hard_get_tempfile():
    # Deprecated, plus I forgot to call Path the first time
    return Path(tempfile.mktemp())

Now, suppose I’m running this tool multiple times, and I’d like to skip running the tool if I already have results. How do I attach some info to the filename?

def easy_get_tempfile(param):
    return CachePath(param, suffix='.txt')  # Path('/tmp/param.txt')
def hard_get_tempfile(param):
    return (Path(tempfile.gettempdir())/param).with_suffix('txt')

Ew. Now, I’m running this tool a lot, maybe even 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:

def easy_get_path(date, person):
    return CachePath(date, person, suffix='_output.txt')
def hard_get_path(date, person):
    personfilename = '{}_output.txt'.format(person)
    returning = Path(tempfile.gettempdir())/date/personfilename
    return returning

Actually, we made a mistake. These aren’t equivalent. We may find out when we pass our path to another tool that it refuses to create the date folder if it doesn’t already exist. This issue can show itself as a Permission Denied error on Unix systems rather than the “File/Folder not found” you might think you would get. Regardless, we figured it out, let’s try again:

def hard_get_path():
    personfilename = '{}_output.txt'.format(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

Now, how do we clear out some day’s results so that we can be sure we’re looking at fresh output of the tool?

def easy_clear_date(date):
    CachePath(date).clear()  # rm -r /tmp/date/*
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:
    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, because we've lost whatever
    # permissions were set on the date folder, or if it were actually a symlink
    # to somewhere else, that's gone now.

And all of this is ignoring the hacky imports you have to do to get pathlib in Py3 and pathlib2 in Py2:

from cachepath import Path  # py2/3
# or
try:
    from pathlib import Path
except:
    from pathlib2 import Path

Convinced yet? pip install cachepath or copy the source into your local utils.py (you know you have one.)

API doc is here.

Shameless Promo

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

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.

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

cachepath-1.1.0.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cachepath-1.1.0-py2.py3-none-any.whl (6.9 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: cachepath-1.1.0.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.3

File hashes

Hashes for cachepath-1.1.0.tar.gz
Algorithm Hash digest
SHA256 0a904873ddfbb28cd7ea212f98ed782486b8d3b220791003cdf6b703db478740
MD5 a187948874530e68f5befce92b0c025a
BLAKE2b-256 2acd4968309a462865f4e3bccce04f8972e7d30bb0567ab613c20124179ef0ea

See more details on using hashes here.

File details

Details for the file cachepath-1.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: cachepath-1.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.3

File hashes

Hashes for cachepath-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9b7541ae9e2a4ef285c3b37ee010dd54283542df72f4df9dbc2f0d297bd7f12c
MD5 79e571db62652259e75fd5a85cc30b8e
BLAKE2b-256 59465a4f116e284348e4bb836f33695cd160a750e17c7435820f9be95095dc28

See more details on using hashes here.

Supported by

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