No project description provided
Project description
Context-Managed Objects in Python
This project provides a way to manage objects in a 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.
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
Hashes for contextmanaged_objects-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | f4366b12d72013afcc6393af8992d657cd4a8323a7fab0ebd7e1d6c14da84c3d |
|
MD5 | 5ef8c2575ebbdc213e9085e11cb0d043 |
|
BLAKE2b-256 | 959b4a01083291288a8c114a342cf001d5b05d307a762a71f36ce483693d9e17 |
Hashes for contextmanaged_objects-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42f14706528b0fda341823b65c5e92b9b038e536686773b48096b1153f73b4d0 |
|
MD5 | c87f1830b084e88fc323323c676eda99 |
|
BLAKE2b-256 | 77b992fe9abe572beeae94076b701a8f9946a5cbdac88dd77f18b637748702ac |