Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

context_managed_singleton-1.0.0.tar.gz (3.0 kB view hashes)

Uploaded Source

Built Distribution

context_managed_singleton-1.0.0-py3-none-any.whl (3.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page