Pattern Utilities¶
The rite.collections.pattern subpackage provides reusable implementations of common design patterns such as singletons, observers, and object pools.
Design Pattern Collections Module¶
Provides design pattern implementations for common software patterns.
Classes:¶
- SingletonMeta: Singleton pattern metaclass
- Observer: Observer pattern implementation
- ObjectPool: Object pool pattern
Classes¶
ObjectPool(factory: Callable[[], Any], max_size: int = 10, reset: Callable[[Any], None] | None = None)
¶
ObjectPool Class¶
A generic object pool for managing reusable objects.
Parameters¶
factory : Callable[[], Any] Factory function to create new objects. max_size : int Maximum number of objects in the pool. reset_func : Callable[[Any], None] | None Optional function to reset objects before reuse.
Initialize an object pool.
factory: Function to create new objects.
max_size: Maximum pool size.
reset_func: Optional function to reset objects.
Functions¶
acquire() -> Any
¶
Acquire an object from the pool.
Any: An object from the pool or newly created.
available_count() -> int
¶
Get number of available objects.
int: Number of available objects.
clear() -> None
¶
Clear all available objects from the pool.
in_use_count() -> int
¶
Get number of objects currently in use.
int: Number of in-use objects.
release(obj: Any) -> None
¶
Release an object back to the pool.
obj: The object to release.
ValueError: If object was not acquired from this pool.
size() -> int
¶
Get total number of objects managed by pool.
int: Total objects (available + in use).
Observable()
¶
Observable Class¶
Base class for observable objects in the Observer pattern.
Initialize an observable object.
Attributes¶
observers: tuple[Observer, ...]
property
¶
Return observers as an immutable tuple.
Functions¶
attach(observer: Observer) -> None
¶
Attach an observer.
observer: The observer to attach.
clear_observers() -> None
¶
Remove all observers.
detach(observer: Observer) -> None
¶
Detach an observer.
observer: The observer to detach.
get_observer_count() -> int
¶
Get the number of attached observers.
int: Number of observers.
notify(*args: Any, **kwargs: Any) -> None
¶
Notify all observers of a change.
*args: Positional arguments to pass to observers.
**kwargs: Keyword arguments to pass to observers.
Observer
¶
SingletonMeta
¶
Bases: type
SingletonMeta Class¶
A metaclass for implementing the Singleton pattern. This ensures that only one instance of a class exists throughout the application lifecycle.
Attributes¶
_instances : dict[type, Any] A dictionary that maps classes to their single instances.
Methods¶
call(*args, **kwargs):
Overrides the __call__ method to control instance creation.
Example¶
# Define a Singleton class
class Configuration(metaclass=SingletonMeta):
def __init__(self, value):
self.value = value
Modules¶
object_pool
¶
Object Pool Pattern¶
Implementation of the Object Pool design pattern for resource management.
Classes¶
ObjectPool(factory: Callable[[], Any], max_size: int = 10, reset: Callable[[Any], None] | None = None)
¶
ObjectPool Class¶
A generic object pool for managing reusable objects.
Parameters¶
factory : Callable[[], Any] Factory function to create new objects. max_size : int Maximum number of objects in the pool. reset_func : Callable[[Any], None] | None Optional function to reset objects before reuse.
Initialize an object pool.
factory: Function to create new objects.
max_size: Maximum pool size.
reset_func: Optional function to reset objects.
Functions¶
acquire() -> Any
¶Acquire an object from the pool.
Any: An object from the pool or newly created.
available_count() -> int
¶Get number of available objects.
int: Number of available objects.
clear() -> None
¶Clear all available objects from the pool.
in_use_count() -> int
¶Get number of objects currently in use.
int: Number of in-use objects.
release(obj: Any) -> None
¶Release an object back to the pool.
obj: The object to release.
ValueError: If object was not acquired from this pool.
size() -> int
¶Get total number of objects managed by pool.
int: Total objects (available + in use).
observer
¶
Observer Pattern¶
Implementation of the Observer design pattern for event-driven programming.
Classes¶
Observable()
¶
Observable Class¶
Base class for observable objects in the Observer pattern.
Initialize an observable object.
Attributes¶
observers: tuple[Observer, ...]
property
¶Return observers as an immutable tuple.
Functions¶
attach(observer: Observer) -> None
¶Attach an observer.
observer: The observer to attach.
clear_observers() -> None
¶Remove all observers.
detach(observer: Observer) -> None
¶Detach an observer.
observer: The observer to detach.
get_observer_count() -> int
¶Get the number of attached observers.
int: Number of observers.
notify(*args: Any, **kwargs: Any) -> None
¶Notify all observers of a change.
*args: Positional arguments to pass to observers.
**kwargs: Keyword arguments to pass to observers.
singleton
¶
Singleton Module¶
This module provides a metaclass SingletonMeta for implementing the Singleton
design pattern. Classes using this metaclass ensure that only one instance of
the class is created, regardless of how many times it is instantiated.
Classes:¶
- SingletonMeta: A metaclass for implementing the Singleton pattern.
Features:¶
- Ensures a single instance of a class.
- Thread-safe by default.
- Provides an easy-to-use interface for creating Singleton classes.
Usage:¶
To use the Singleton pattern, define your class with SingletonMeta as its
metaclass:
class MySingleton(metaclass=SingletonMeta):
pass # pylint: disable=unnecessary-pass
Example:¶
# Define a Singleton class
class Configuration(metaclass=SingletonMeta):
def __init__(self, value):
self.value = value
# Test the Singleton behavior
config1 = Configuration("value1")
config2 = Configuration("value2")
assert config1 is config2 # True
print(config1.value) # Outputs: value2
Classes¶
Configuration(value: str)
¶
A Singleton class for storing configuration values.
SingletonMeta
¶
Bases: type
SingletonMeta Class¶
A metaclass for implementing the Singleton pattern. This ensures that only one instance of a class exists throughout the application lifecycle.
Attributes¶
_instances : dict[type, Any] A dictionary that maps classes to their single instances.
Methods¶
call(*args, **kwargs):
Overrides the __call__ method to control instance creation.
Example¶
# Define a Singleton class
class Configuration(metaclass=SingletonMeta):
def __init__(self, value):
self.value = value
options: show_root_heading: true show_source: false heading_level: 2