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 details)

Uploaded Source

Built Distribution

File details

Details for the file context_managed_singleton-1.0.0.tar.gz.

File metadata

File hashes

Hashes for context_managed_singleton-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e98a7412f1adf99e3f43a50f79915dbf018f7333a0bb94cc680f8be8968239fc
MD5 cfc02fbb68b420232628aa8cc155988d
BLAKE2b-256 be25dc0c9611973d6bb6764922a73d41111a87b24a1aa7667c8c2d0092f54844

See more details on using hashes here.

File details

Details for the file context_managed_singleton-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for context_managed_singleton-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 14d878191f5f58ebf4b72bc6c32987bee35b87830e36c9bdc1a1ae320ccbb2f9
MD5 cab0a70100cfcd6642dc9dde2db8b86a
BLAKE2b-256 e8cf6660845584ef7c14a0163ed2361fb705cbe21be79b42984a9fbad28bdf9c

See more details on using hashes here.

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