Skip to main content

bagof-paths

One path API for local files and the cloud.

Wrap a path or a URL, then use it like pathlib.Path. The same code reads a local file, an object on S3, or anything the underlying libraries reach.

from bagof.paths import Path

Path("/data/train.zarr")           # a local file
Path("s3://my-bucket/train.zarr")  # an object on S3

Both give you the same methods: read_bytes, exists, iterdir, /, and the rest of the pathlib surface.

>>> from bagof.paths import Path
>>> p = Path("/data/sets/train.zarr")
>>> p.name
'train.zarr'
>>> p.parent
Path('/data/sets')
>>> p / "chunks"
Path('/data/sets/train.zarr/chunks')

Features

  • One API for every path. Local files, cloud storage, and unknown backends all use the same methods.
  • Missing methods are filled in. When a library lacks a method, bagof.paths builds it from simpler ones. It reads text from bytes, and copies a folder by copying its files.
  • One error to catch. An operation that cannot work raises a single UnsupportedPathOperation, the same for every library.
  • Async support. AsyncPath gives you the same methods with await.
  • No required dependency. Local paths use only the standard library.

Installation

pip install bagof-paths          # local paths
pip install bagof-paths[upath]   # add remote paths via universal-pathlib
pip install bagof-paths[cloud]   # add cloud paths via cloudpathlib

A remote store also needs its own library, such as s3fs for s3:// or cloudpathlib[s3].

The same code, local or remote

Path reads the start of the string to choose a backend. A plain path is a local file. A URL uses universal-pathlib, or cloudpathlib if you have it.

from bagof.paths import Path

def load(location: str) -> bytes:
    return Path(location).read_bytes()

load("/data/train.bin")
load("s3://my-bucket/train.bin")

Credentials

Pass connection details with storage_options. They go straight to the backend.

from bagof.paths import Path

Path(
    "s3://my-bucket/train.bin",
    storage_options={"key": "AKIA...", "secret": "...", "endpoint_url": "..."},
)

Leave storage_options off to use ambient credentials, such as environment variables, ~/.aws/config, or an instance role.

Set defaults once for a scheme with set_storage_options, and a per-call storage_options overrides them key by key.

from bagof.paths import set_storage_options

set_storage_options("s3", {"endpoint_url": "https://minio.local"})

Async

AsyncPath turns the methods that touch storage into coroutines. The methods that only describe a path (name, parent, /) stay synchronous.

import asyncio
from bagof.paths import AsyncPath

async def main() -> None:
    p = AsyncPath("s3://my-bucket/train.bin")
    if await p.exists():
        data = await p.read_bytes()

asyncio.run(main())

For a cloud store whose library speaks async (such as s3:// through s3fs), AsyncPath talks to it directly, with no threads. For every other path it runs the synchronous driver in a worker thread, so the same code works either way.

Learn more

See how it compares to pathlib, UPath, and AnyPath.

Metadata

Release files for bagof-paths 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bagof-paths 0.1.0
File Size Uploaded
bagof_paths-0.1.0.tar.gz 77.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bagof-paths 0.1.0
File Interpreter ABI Platform
bagof_paths-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 131.2 kB

Release files / bagof_paths-0.1.0.tar.gz

Download URL bagof_paths-0.1.0.tar.gz
Size 77.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a435e53de4656523c98a2e5b93047df30cf85b1ac8c81afeed78824310c4fde5
BLAKE2b-256 checksum
How to use checksums
1c01730dcf139fc7464eedc0f02406ffaad43e58c348d244453d4b764e201b37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / bagof_paths-0.1.0-py3-none-any.whl

Download URL bagof_paths-0.1.0-py3-none-any.whl
Size 54.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7f3a6d360e37ae361e074ef3629cd53280d73d465b583dbd1f9347979d3690ff
BLAKE2b-256 checksum
How to use checksums
d803d34cc97ce786c753122d7ee4f44f7b6320b433cb6cca7f6174931fda7d0b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

0.2.0

2 release files

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page