No project description provided
Project description
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.
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 contextmanaged_objects-1.0.0.tar.gz
.
File metadata
- Download URL: contextmanaged_objects-1.0.0.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d38a8bf794b2cb0eb7fb77581bb6b64149ed28aa357c7cbd946795b8ee9b34bb |
|
MD5 | 79f0b66ac943bf373643fb9bd47d4d95 |
|
BLAKE2b-256 | 7ff48eca9ffa66104f82c0ffd582ead439b5fde9aa914b6d1ac9c043b461f492 |
File details
Details for the file contextmanaged_objects-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: contextmanaged_objects-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cebeee63a9332676317961497cc318b6c638400b68e972a668e2525aadda1d09 |
|
MD5 | 814d019a6a6e6a85412eec6c6851a0c0 |
|
BLAKE2b-256 | 23f9678ae72ddafb90d5b0dbb30791ef544fdb38fd270999bde6dcef778a0a07 |