Registry

The main operator registry class and related exceptions.

exception jsonlogic.registry.AlreadyRegistered(operator_id: str, /)

The provided ID is already registered.

exception jsonlogic.registry.UnknownOperator(operator_id: str, /)

The provided ID does not exist in the registry.

class jsonlogic.registry.OperatorRegistry

A collection of Operator classes.

Acts as a wrapper over a mapping of operator IDs to the actual class.

>>> reg = OperatorRegistry()
>>> reg.register("var", MyVarOperator)
>>> reg.get("var")
<class 'MyVarOperator'>
>>> reg.get("unknown")
Traceback (most recent call last):
...
UnknownOperator: "unknown"
register(operator_id: str, *, force: bool = False) Callable[[OperatorTypeT], OperatorTypeT]
register(operator_id: str, operator_type: OperatorTypeT, *, force: bool = False) OperatorTypeT

Register an operator type under the provided ID.

Parameters:
  • operator_id – The ID to be used to register the operator.

  • operator_type – The class object of the operator. If not provided, will return a callable to be applied on an operator class.

  • force – Whether to override any existing operator under the provided ID.

Raises:

AlreadyRegistered – If force wasn’t set and the ID already exists.

Note

The method is usable as a decorator:

reg = OperatorRegistry()
reg.register("==", EqualOperator)

# Or:
@reg.register("==")
class EqualOperator(Operator): ...
get(operator_id: str, /) type[Operator]

Get the operator class corresponding to the ID.

Parameters:

operator_id – The registered ID of the operator.

Raises:

UnknownOperator – If the provided ID does not exist.

remove(operator_id: str, /) None

Remove the operator from the registry.

Parameters:

operator_id – The registered ID of the operator to be removed.

copy(*, extend: OperatorRegistry | dict[str, type[Operator]] | None = None, force: bool = False) Self

Create a new instance of the registry.

Parameters:
  • extend – A registry or a mapping to use to register new operators while doing the copy.

  • force – Whether to override any existing operator under the provided ID.

Returns:

A new instance of the registry.