Skip to main content

No project description provided

Project description

pyclsload

CodeFactor

Lightweight helpers to load classes from Python source files and directories with sensible defaults, clear errors, and a tiny CLI.

Why

Avoid repeating the importlib boilerplate when you just want a class from a .py file:

from pathlib import Path
from pyclsload import load_cls

inst = load_cls(Path("somefile.py"), "MyClass", foo=1, bar=2)
print(inst)

Features

  • Load a class from a file with load_cls(path, name, *args, **kwargs)
  • Load all classes defined in a directory with load_dir(directory, arguments=None, on_collision=...)
    • Only *.py files, skips files starting with _ (like __init__.py)
    • Deterministic ordering (lexicographic by filename)
    • Per-class kwargs via the arguments mapping
    • Collision policy: overwrite (default), keep_first, or error
  • Helpful errors (FileNotFoundError, ModuleLoadError, ClassNotFoundError)
  • Simple in-process cache to avoid re-executing the same file repeatedly
  • Small CLI to try things out quickly

Usage

Load a single class

from pathlib import Path
from pyclsload import load_cls

inst = load_cls(Path("plugins/example.py"), "Plugin", answer=42)
print(inst)

Load a directory of classes

from pathlib import Path
from pyclsload import load_dir

instances = load_dir(Path("plugins"), {
    "Example": {"answer": 42},
})
print(instances["Example"])  # -> Plugin instance

CLI

Install the package and run:

python -m pyclsload -f path/to/file.py -c ClassName -m run -fa x=1 y=2

Or list and instantiate all classes from a directory:

python -m pyclsload -d path/to/dir

Notes:

  • --class-arguments and --function-arguments accept space-separated tokens. Use key=value for kwargs; tokens without = are passed as positional strings.

Safety

Executing arbitrary Python files will execute code at import time. Only load trusted code.

Install

pip install pyclsload

See the tests for usage examples.

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

pyclsload-1.6.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

pyclsload-1.6.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file pyclsload-1.6.0.tar.gz.

File metadata

  • Download URL: pyclsload-1.6.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyclsload-1.6.0.tar.gz
Algorithm Hash digest
SHA256 10c438354ef3e38c3434015ef5e32e6b92a1dde6050871c384aa665c08e022c6
MD5 c87f01a3587f9c709ab5248f114c16f2
BLAKE2b-256 013c00a01df6719880c8b14e85da09a5cee06bf0bc586d49a0851bbcbfcb88f9

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyclsload-1.6.0.tar.gz:

Publisher: publish.yml on nbdy/pyclsload

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyclsload-1.6.0-py3-none-any.whl.

File metadata

  • Download URL: pyclsload-1.6.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyclsload-1.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f02a5ffcfca19240afdd57c1ebf34edec9d9679d686e15b593f7fb03d385c831
MD5 18f4e98949b999dd5585ef5570eae0f2
BLAKE2b-256 ca46f0186ddb03da1cee055a5041d1f75c7f9f965c30569595e0a876b5f8e64f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyclsload-1.6.0-py3-none-any.whl:

Publisher: publish.yml on nbdy/pyclsload

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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