Singleton

Singleton helpers and boilerplate

class orion.core.utils.singleton.AbstractSingletonType(name, bases, namespace, **kwargs)[source]

This will create singleton base classes, that need to be subclassed and implemented.

class orion.core.utils.singleton.GenericSingletonFactory(base)[source]

Factory to create singleton instances of classes inheriting a given base class.

Methods

create([of_type])

Create an object, instance of self.base

create(of_type=None, *args, **kwargs)[source]

Create an object, instance of self.base

If the instance is already created, self.create can only be called without arguments and will return the singleton.

Cannot be called without arguments if the singleton was not already created.

Parameters
of_type: str, optional

Name of class, subclass of self.base. Capitalization insensitive.

args: *

Positional arguments to construct the givin class.

kwargs: **

Keyword arguments to construct the givin class.

Raises
SingletonNotInstantiatedError
  • If self.create() was never called and is called without arguments for the first time.

  • If self.create() was never called and the current call raises an error.

SingletonAlreadyInstantiatedError

If self.create() was already called with arguments (the singleton exist) and is called again with arguments.

exception orion.core.utils.singleton.SingletonAlreadyInstantiatedError(name)[source]

Exception to be raised when someone provides arguments to build an object from a already-instantiated SingletonType class.

class orion.core.utils.singleton.SingletonFactory(name, bases, namespace, **kwargs)[source]

Wrapping orion.core.utils.Factory with SingletonType. Keep compatibility with AbstractSingletonType.

exception orion.core.utils.singleton.SingletonNotInstantiatedError(name)[source]

Exception to be raised when someone try to access an instance of a singleton that has not been instantiated yet

class orion.core.utils.singleton.SingletonType(name, bases, dictionary)[source]

Metaclass that implements the singleton pattern for a Python class.

Methods

__call__(*args, **kwargs)

Create an object if does not already exist, otherwise return what there is.

__call__(*args, **kwargs)[source]

Create an object if does not already exist, otherwise return what there is.