Skip to main content

Context-Managed Objects in Python

This project provides a way to manage objects in a global or thread-wise context-aware manner in Python. It includes a class HasContextManagedFocus and a decorator make_current. These tools can be particularly useful in scenarios where you need to manage context-specific objects like user credentials or other parameters that are frequently passed around in a backend application.

Getting Started

This project is available on PyPI. You can install it using pip:

pip install contextmanaged-objects

Usage

Consider a scenario where you're building a web application and you need to keep track of the current user's credentials. Instead of passing the user credentials as arguments to every function, you can use HasContextManagedFocus and make_current to manage the current user context.

from contextmanaged_objects import HasContextManagedFocus, make_current

class User(HasContextManagedFocus):
    def __init__(self, username, password):
        self.username = username
        self.password = password

    @make_current()
    def start_doing_something(self):
        # do something with the current user
        call_first_library_function()

...

fiftieth_library_function():
    # do something with the current user
    current_user = User.get_current()
    # we didn't have to pass current_user as an argument along the entire 1st-49th library function chain

user1 = User('user1', 'password1')
user2 = User('user2', 'password2')

In this example, User is a subclass of HasContextManagedFocus. The do_something method is decorated with make_current, so whenever do_something is invoked, user1 or user2 (whichever is the current instance) is placed on top of the context stack. This allows you to easily manage the current user context without having to pass user credentials around.

API Reference

HasContextManagedFocus

This class allows objects to be context-managed. It maintains a context stack where instances can be placed on top of the stack and retrieved. The as_current method is a context manager that puts the instance on top of its context stack.

make_current

This function is a decorator for instance methods. It automatically puts the instance on top of its context stack during invocation and removes it after invocation is over.

Multithreading

Call the .enable_threaded_context() or .disable_threaded_context() to enable or disable thread-specific context management. By default, threaded context management is disabled.

from contextmanaged_objects import HasContextManagedFocus, make_current

HasContextManagedFocus.enable_threaded_context()

...

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

We use pytest for testing. To run the tests, run the following command from the root directory of the project:

poetry run pytest

Please make sure to update tests as appropriate.

License

We use the MIT license. If you make contributions to this project, you agree to license your contributions under the MIT license and you may include your name in the list of authors.

Metadata

Release files for contextmanaged-objects 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for contextmanaged-objects 1.0.0
File Size Uploaded Provenance
contextmanaged_objects-1.0.0.tar.gz 3.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for contextmanaged-objects 1.0.0
File Interpreter ABI Platform
contextmanaged_objects-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.7 kB

Release files / contextmanaged_objects-1.0.0.tar.gz

Download URL contextmanaged_objects-1.0.0.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d38a8bf794b2cb0eb7fb77581bb6b64149ed28aa357c7cbd946795b8ee9b34bb
BLAKE2b-256 checksum
How to use checksums
7ff48eca9ffa66104f82c0ffd582ead439b5fde9aa914b6d1ac9c043b461f492
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release files / contextmanaged_objects-1.0.0-py3-none-any.whl

Download URL contextmanaged_objects-1.0.0-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cebeee63a9332676317961497cc318b6c638400b68e972a668e2525aadda1d09
BLAKE2b-256 checksum
How to use checksums
23f9678ae72ddafb90d5b0dbb30791ef544fdb38fd270999bde6dcef778a0a07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Security for contextmanaged-objects 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page