Simple stack-based, context managed singleton-ish utility
Project description
Context Managed Singleton This is a Python utility module that provides a base class for creating a singleton that can be used in a context manager. The purpose of the context manager is to allow for easy management of the state of the singleton in a multithreaded environment where the singleton needs to be accessed and modified by multiple threads simultaneously.
Usage
Basic Usage
from context_managed_singleton import ContextManagedSingleton
# Create an instance of ContextManagedSingleton
ctx = ContextManagedSingleton()
# Use the instance in a with block
with ctx:
# Do something with the singleton
...
# The instance is automatically removed from the stack when the with block is exited
Advanced Usage
The ContextManagedSingleton class can be subclassed to create a custom singleton with additional functionality. The class can also be wrapped around an existing class using the wrap static method.
from context_managed_singleton import ContextManagedSingleton
# Subclass the ContextManagedSingleton class
class MyClass(ContextManagedSingleton):
...
# (alternatively, wrap with ContextManagedSingleton.wrap decorator)
# @ContextManagedSingleton.wrap
# class MyClass:
def foo():
# don't bother forwarding the instance to foo()
# just get the current instance from the .current() class method
my_instance = MyClass.current()
...
with MySingleton():
# lots of code and deeply buried calls to foo()
...
Nested Contexts
The ContextManagedSingleton class can be used in nested contexts.
from context_managed_singleton import ContextManagedSingleton
# Create an instance of ContextManagedSingleton
ctx = ContextManagedSingleton()
# Use the instance in a with block
with ctx:
# Do something with the singleton
...
# Create a nested context
with ctx:
# Do something with the nested singleton
...
# The instances are automatically removed from the stack when the with blocks are exited
Recursive Attribute Access
The ContextManagedSingleton class supports recursive attribute access. When an attribute is not found on the current instance, the class will look for the attribute on the previous instance in the stack.
from context_managed_singleton import ContextManagedSingleton
# create and enter a context
with ContextManagedSingleton({"a": 1, "b": 2}):
with ContextManagedSingleton({"a": 4}, getattr_recursive=True):
print(ContextManagedSingleton.current().a) # prints 4
print(ContextManagedSingleton.current().b) # prints 2
print(ContextManagedSingleton.current().a) # prints 1
print(ContextManagedSingleton.current().b) # prints 2
# The instances are automatically removed from the stack when the with blocks are exited
Tests
The tests for the ContextManagedSingleton class can be found in the tests directory. To run the tests, run the following command:
python -m unittest discover tests
License
This project is licensed under the MIT License. See the LICENSE file for more information.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file context_managed_singleton-1.0.0.tar.gz
.
File metadata
- Download URL: context_managed_singleton-1.0.0.tar.gz
- Upload date:
- Size: 3.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e98a7412f1adf99e3f43a50f79915dbf018f7333a0bb94cc680f8be8968239fc |
|
MD5 | cfc02fbb68b420232628aa8cc155988d |
|
BLAKE2b-256 | be25dc0c9611973d6bb6764922a73d41111a87b24a1aa7667c8c2d0092f54844 |
File details
Details for the file context_managed_singleton-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: context_managed_singleton-1.0.0-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 14d878191f5f58ebf4b72bc6c32987bee35b87830e36c9bdc1a1ae320ccbb2f9 |
|
MD5 | cab0a70100cfcd6642dc9dde2db8b86a |
|
BLAKE2b-256 | e8cf6660845584ef7c14a0163ed2361fb705cbe21be79b42984a9fbad28bdf9c |