edgegraph.structure.singleton#
Provides metaclasses to assist creating singletons.
This module contains helper metaclasses to ease creating custom subclasses of
Vertex, or anything else you might want
to singleton-ize. It provides two helpers, one for creating a so-called “true
singleton”, and one for creating so-called “semi-singletons”:
Global singleton: there can be only one instance of global singletons. See
TrueSingleton.Semi-singletons: there can be multiple instances of semi-singleton classes, but they are designated with some unique primary key. Only one instance with a given primary key can exist. See
semi_singleton_metaclass().
Consider carefully how you intend to use these tools!
Some disclaiming about singleton usage (anti-)patterns is worthwile. Many people feel that singletons in general violate good OOP practices, and often with good reason. The purpose of OOP is to provide instances of objects that behave independently of each other – singletons violate this.
Nonetheless, I (and many others) argue that in moderation, singletons can be used to good effect. There are some use-cases that see frequent singletons (loggers and global configuration containers, for example) and improve code quality with it. How exactly you intend to apply this logic to … graphs … is up to you – but edgegraph will happily supply the foot-gun.
In order to sustain the law of least astonishment, these helpers take the form of metaclasses. There are several ways to implement singletons in Python, but after some surveying (Googling) I decided that metaclasses approach gave:
Cleanest implementation
Most straightforward to use (“pythonic”)
Best expression of intent
Functions
|
Clears a specified semi-singleton. |
|
Clears TrueSingleton cache for either a specified type, or all TrueSingleton types. |
Get all instances belonging to a given semi-singleton type. |
|
|
Generate and return a metaclass for semi-singletons. |
Classes
Metaclass for true singletons. |