Skip to main content

Access resources on your terms

Project description

Acres: Access resources on your terms

This module aims to provide a simple way to access package resources that will fit most use cases.

The problem: importlib.resources provides a composable but ugly interface:

from importlib.resources import files, as_file

with as_file(files(my_module) / 'data' / 'resource.ext') as resource_path:
    # Interact with resource_path as a pathlib.Path

The files() and as_file() functions do not make the meanings obvious, and the different use cases do not obviously map on to these names.

The solution: acres.Loader is a class that provides read, filesystem and cached filesystem access to package resources.

Module data loader

Suppose you have a module structure:

src/
  mypkg/
    data/
      resourceDir/
        ...
      __init__.py
      resource.ext
    __init__.py
    ...

In src/mypkg/data/__init__.py, add:

'''Data package

.. autofunction:: load_resource

.. automethod:: load_resource.readable

.. automethod:: load_resource.as_path

.. automethod:: load_resource.cached
'''

from acres import Loader

load_resource = Loader(__package__)

mypkg.data.load_resource is now a function that will return a Path to a resource that is guaranteed to exist until interpreter exit:

from mypkg.data import load_resource

resource_file: Path = load_resource('resource.ext')

For additional control, you can use load_resource.readable() to return a Path-like object that implements .read_text() and .read_bytes():

resource_contents: bytes = load_resource.readable('resource.ext').read_bytes()

Or a context manager with a limited lifetime:

with load_resource.as_path('resourceDir') as resource_dir:
    # Work with the contents of `resource_dir` as a `Path`

# Outside the `with` block, `resource_dir` may no longer point to an existing path.

Note that load_resource() is a shorthand for load_resource.cached(), whose explicitness might be more to your taste.

On-demand data loading

acres may be used as a library to work with any package:

from acres import Loader
import somepkg

text: str = Loader(somepkg).readable('data/someresource.txt').read_text()

with Loader(somepkg).as_path('data') as somepkgdata:
    walk_dir(somepkgdata)

If Loader().cached() is used, the resources will remain available until the interpreter exists, even if the Loader instance is garbage collected.

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

acres-0.1.0.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

acres-0.1.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file acres-0.1.0.tar.gz.

File metadata

  • Download URL: acres-0.1.0.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for acres-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4765479683389849368947da9e5319e677e7323ed858d642f9736ad1c070f45b
MD5 a1d90d9c4f254316bc105b9266e780c4
BLAKE2b-256 b7ce76ca23e81d650fa90b558a882007eb563a0bbb135d1e2f3959aa92ffb4bb

See more details on using hashes here.

File details

Details for the file acres-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: acres-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for acres-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7bbb3744de84d1499e5cc00f02d10f7e85c880e343722871816ca41502d4d103
MD5 670af62973f097cd7dae31d4618a7a7a
BLAKE2b-256 ef76341a0bec7d8159569eee18c9838cad0802a1c31775c195d5a146bdcdd1bb

See more details on using hashes here.

Supported by

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