pyexperiment.utils.Singleton

Provides data structures for unique objects.

The Singleton class whose implementation is inspired by Tornado’s singleton (tornado.ioloop.IOLoop.instance()), can be inherited by classes which only need to be instantiated once (for example a global settings class such as pyexperiment.Config). This design pattern is often criticized, but it is hard to beat in terms of simplicity.

A variant of the Singleton, the DefaultSingleton provides an abstract base class for classes that are only instantiated once, but need to provide an instance before being properly initialized (such as pyexperiment.Logger.Logger).

The function delegate_singleton ‘thunks’ a Singleton so that calls to the singleton instance’s methods don’t need to be ugly chain calls.

Written by Peter Duerr (Singleton inspired by Tornado’s implementation)

class pyexperiment.utils.Singleton.DefaultSingleton[source][source]

ABC for singleton that does not automatically initialize

If get_instance is called on an uninitialized DefaultSingleton, a pseudo-instance is returned.

Sub-classes need to implement the function _get_pseudo_instance that returns a pseudo instance.

classmethod get_instance()[source][source]

Get the singleton instance if its initialized. Returns, the pseudo instance if not.

classmethod initialize(*args, **kwargs)[source][source]

Initializes the singleton. After calling this function, the real instance will be used.

classmethod reset_instance()[source][source]

Reset the singleton instance if its initialized.

class pyexperiment.utils.Singleton.Singleton[source][source]

Singleton base-class (or mixin)

classmethod get_instance()[source][source]

Get the singleton instance

classmethod reset_instance()[source][source]

Reset the singleton

pyexperiment.utils.Singleton.delegate_singleton(singleton)[source][source]

Creates an object that delegates all calls to the singleton

pyexperiment.utils.Singleton.delegate_special_methods(singleton)[source][source]

Decorator that delegates special methods to a singleton