ek.registry#
Strategy registry, optional-dependency guarding, and requirement checks.
ek is open-closed: every swappable behaviour (a Metric, a
Signal, a calibrator, an OCR backend, …) is registered under a
namespace and resolved by name. Built-in strategies register themselves at
import time; third parties add their own either by calling register() or by
declaring an entry point in the ek.<namespace> group – no edit to ek needed.
Two more concerns live here so they stay out of the core logic:
requires_extra()– a decorator that turns a missing optional dependency into an actionable error (pip install ek[ocr]) instead of an opaqueImportErrorraised deep inside a call.check_requirements()– a helper that reports, without raising, what an engine/feature needs (Python packages, system binaries, credentials), and defers toocracy’s own doctor for OCR engines when it is installed.
Example
>>> @register("metrics", "shout")
... def shout(pred, gold, *, grammar=None):
... return pred == gold
>>> get("metrics", "shout")("a", "a")
True
>>> "shout" in names("metrics")
True
Functions
|
Report what an OCR engine or feature extra needs, without raising. |
|
Resolve a registered strategy by name (discovering entry points first). |
|
All registered names in a namespace (after entry-point discovery). |
|
Register |
|
Decorator: fail with an actionable install hint if an extra is missing. |
|
Coerce a strategy reference to a callable. |
Exceptions
Raised when an optional feature is used without its extra installed. |
- exception ek.registry.MissingExtraError[source]#
Bases:
ImportErrorRaised when an optional feature is used without its extra installed.
- ek.registry.check_requirements(*, engine=None, extra=None)[source]#
Report what an OCR engine or feature extra needs, without raising.
For an OCR
engineand an installedocracy, defers toocracy’s own doctor (which knows each backend’s pip package, system binaries and credentials). Otherwise returns a generic hint.- Return type:
- Returns:
A dict with at least
{"ok": bool, "hint": str}.
- ek.registry.get(namespace, name)[source]#
Resolve a registered strategy by name (discovering entry points first).
- ek.registry.names(namespace)[source]#
All registered names in a namespace (after entry-point discovery).
- ek.registry.register(namespace, name, obj=None)[source]#
Register
objundernamespace/name(usable as a decorator).- Parameters:
- Return type:
- Returns:
obj(so it can be used as a decorator).
- ek.registry.requires_extra(extra, *, packages=None)[source]#
Decorator: fail with an actionable install hint if an extra is missing.
The wrapped callable runs only once every package in
packagesimports; the common case (everything installed) adds a single cheap import check.- Parameters:
- Return type:
Example
>>> @requires_extra("ocr", packages=["definitely_not_installed_pkg"]) ... def run(): ... return "ran" >>> try: ... run() ... except MissingExtraError as e: ... print("ek[ocr]" in str(e)) True