Skip to main content

A tiny thread-safe registry mapping for simple plugin/registry use-cases.

Project description

mnemoreg

A tiny, dependency-free, thread-safe registry mapping useful for registering callables and values by string keys. Designed to be embedded in other projects or used as a small standalone utility.

Key points

  • Small, stdlib-only implementation.
  • Thread-safe (uses RLock for mutations).
  • Simple decorator-based registration for callables.

Quick facts

  • Package exports: Registry, AlreadyRegisteredError, NotRegisteredError, StorageProtocol, and __version__.
  • Main implementation: mnemoreg/core.py.
  • Storage helpers live in mnemoreg/_storage (default in-memory backend is exported as MemoeryStorage — note: the name contains a historical typo).

Install

From PyPI:

pip install mnemoreg

From source (editable/development):

git clone https://github.com/i3iorn/mnemoreg.git
cd mnemoreg
python -m venv .venv
# Windows cmd.exe:
.venv\Scripts\activate
# PowerShell:
.venv\Scripts\Activate.ps1
# Unix/macOS:
source .venv/bin/activate

pip install -e .
# dev extras:
pip install -e .[dev]

Usage (quick)

The Registry behaves like a mapping from string keys to values. Examples:

from mnemoreg import Registry, OverwritePolicy

r = Registry[str, int]()
r['one'] = 1
assert r['one'] == 1

# decorator registration (explicit key)
@r.register('plus')
def plus(x: int) -> int:
    return x + 1

assert r['plus'](4) == 5

# decorator registration (uses function.__name__ when key omitted)
@r.register()
def multiply(x: int, y: int) -> int:
    return x * y

assert r['multiply'](3, 4) == 12

# bulk operations (acquires the same lock for the block)
with r.bulk():
    r['a'] = 1
    r['b'] = 2

# removal
del r['a']  # there is no `unregister` helper; use deletion

# snapshots / serialization
snap = r.snapshot()      # shallow dict copy
s = r.to_json()          # JSON string (shallow)
new_r = Registry.from_json(s)

API / behavior summary

  • class Registry(Generic[K, V])

    • Mapping-like: __getitem__, __setitem__, __delitem__, __iter__, __len__, __contains__.
    • register(key: Optional[str] = None) — decorator to register functions/values.
    • get(key, default=None), snapshot(), to_dict() — read helpers.
    • from_dict(mapping), from_json(s) — classmethods to build a Registry from serialized data.
    • to_json(**kwargs) — serialize shallowly to JSON.
    • bulk() — context manager that acquires the registry lock for batched operations.
    • update(mapping), clear().
  • Overwrite behavior: controlled by OverwritePolicy (FORBID = 0, ALLOW = 1, WARN = 2). The default is FORBID.

  • Exceptions: AlreadyRegisteredError, NotRegisteredError.

Notes and caveats (important)

  • The public API does not include unregister() or remove() — remove entries with del registry[key].
  • The storage package exports MemoeryStorage (typo). It's internal and only relevant when passing a custom store= to Registry.
  • Registry expects string keys (type bound K=str) and will raise on invalid keys (empty, contains whitespace, or wrong type).

Development

This project uses pytest, ruff, black, isort, mypy, and pre-commit hooks.

Typical local developer setup:

python -m venv .venv
# activate the venv
pip install -e .[dev]

Run tests:

python -m pytest -q

Format / lint / checks (examples):

ruff check --fix .
isort --profile black .
black .
pre-commit run --all-files
python -m mypy mnemoreg --ignore-missing-imports

Pre-commit is configured in .pre-commit-config.yaml, and mypy/ruff settings live in pyproject.toml.

CI & publishing

There are GitHub Actions workflows under .github/workflows/.

The publish.yml workflow (current behavior):

  • Triggers on: push to main or master, tag pushes matching v*.*.*, pull requests to main/master, and workflow_dispatch.
  • The workflow runs tests, then attempts to determine/create a semver tag and push it, builds distributions, and (optionally) uploads to PyPI if the MNEMOREG_PY_PI_TOKEN secret is configured.

If you don't want to trigger the publish flow or accidental publishes:

  • Work from a fork and open PRs from the fork — forks cannot push tags to the upstream repo and won't have the PyPI secret.
  • Work on non-main branches; pushes to non-main branches do not trigger the push trigger (but PRs to master do trigger pull_request).
  • Repository maintainers can tighten the workflow triggers (recommended): e.g. remove pull_request trigger, only run publish on explicit tag push or manual workflow_dispatch.
  • Ensure MNEMOREG_PY_PI_TOKEN is never added unless you intend to publish.

Contributing

Please follow the project's code style (black/ruff/isort) and add tests for new behavior. Suggested flow for external contributors:

  • Fork the repo.
  • Create a feature branch in your fork and open a PR to master.
  • Add tests and run the test suite locally.

License

MIT — see the LICENSE file.

Maintainers

i3iorn

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

mnemoreg-0.3.7.tar.gz (24.7 kB view details)

Uploaded Source

Built Distribution

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

mnemoreg-0.3.7-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file mnemoreg-0.3.7.tar.gz.

File metadata

  • Download URL: mnemoreg-0.3.7.tar.gz
  • Upload date:
  • Size: 24.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for mnemoreg-0.3.7.tar.gz
Algorithm Hash digest
SHA256 b2eab8f341485a89b73763476d46263209c3c7a3d383746a4327c55ba4817887
MD5 df8c5129ffdb88158a1c60d588ad7442
BLAKE2b-256 fe4e0009bee65a524af490e9a09fd0db1fdaca6eb477b4410a15851182d1f908

See more details on using hashes here.

File details

Details for the file mnemoreg-0.3.7-py3-none-any.whl.

File metadata

  • Download URL: mnemoreg-0.3.7-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for mnemoreg-0.3.7-py3-none-any.whl
Algorithm Hash digest
SHA256 60c4129bc8c6a6f2427e518b64fc81bf10b283dcb27eb52920d2fbea363760f5
MD5 4f2a32ef79dbb0c4a5b5ccecf52ebefc
BLAKE2b-256 2fadfd36736d791d89b91f075401c0bf73d8b6f0cc46ce911c34c51c9138ad97

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