Unique Objects

Core function and decorator for look-up object instantiation. Adapted from: https://github.com/sqlalchemy/sqlalchemy/wiki/UniqueObject

unique(index: Indexer, cls: Type, constructor: Callable, args: tuple, kwargs: dict) Any[source]

Check and return existing instance if present, else create it.

Parameters:
  • index (Indexer) – The almirah index used to store cached instances.

  • cls (Type) – The class of the object being instantiated.

  • constructor (Callable) – A callable used to create the object if not found.

  • args (tuple) – Positional arguments (should be empty for this usage).

  • kwargs (dict) – Keyword arguments used to instantiate the class.

Returns:

An instance of cls.

Return type:

Any

Raises:

TypeError – If positional arguments are given or required keyword arguments are missing.

uniquify(index: Any) Callable[source]

Class decorator to augment classes for look-up instantiation.

Parameters:

index (Indexer) – The index used to manage unique instances.

Returns:

A decorator that modifies a class to use unique instantiation.

Return type:

Callable