nr.proxy
The nr.proxy module provides utilities for creating Python object proxies which will forward
almost any interaction with the object to another object. This allows for the creation of very
convenient APIs in Python applications (e.g. à la Flask request object).
This package requires Python 3.5 or higher.
Example: proxy
Command-line interfaces built with Click can use the Context.ensure_object() method to
attach information to a context. Retrieving that information from the context include a couple
of lines which over all reduce the readability of the code. Using a proxy allows you to access
that information once it is initialized in the context as if the data was available globally.
import click
import nr.proxy
from pathlib import Path
from .config import Configuration
config = nr.proxy.proxy[Configuration](lambda: click.get_current_context().obj['config'])
@click.group()
@click.option('-c', '--config', 'config_file', type=Path, default='config.toml',
default='Path to the configuration file.')
@click.pass_context
def cli(ctx: click.Context, config: Path) -> None:
ctx.ensure_object(dict)['config'] = Configuration.load(config)
@cli.command()
def validate():
# No need to use @click.pass_context or access ctx.obj['config'].
config.validate()
Example: threadlocal
The below is an example for creating a globally accessible SqlAlchemy session. Inside a
with-context using make_session(), the global session object can be accesses like a normal
instance of the Session class. Outside of the context, accessing the session object results
in a RuntimeError with the specified error message.
The advantage of this method is that the Session object does not need to be passed around,
but can instead just be accessed globally.
import contextlib
import nr.proxy
from sqlalchemy.orm import sessionmaker
from typing import Generator
Session = sessionmaker()
session = nr.proxy.threadlocal[Session](
name=__name__ + '.session',
error_message=
'({name}) No SqlAlchemy session is available. Ensure that you are using the '
'make_session() context manager before accessing the global session proxy.',
)
@contextlib.contextmanager
def make_session() -> Generator[None, None, None]:
"""
A context manager that creates a new #Session object and makes it available in the global
#session proxy object.
"""
nr.proxy.push(session, Session())
try:
yield
except:
session.rollback()
raise
else:
session.commit()
finally:
nr.proxy.pop(session)
Copyright © 2020 Niklas Rosenstein
Metadata
Release files for nr.proxy 1.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| nr.proxy-1.1.1.tar.gz | 9.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| nr.proxy-1.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.3 kB
Release files / nr.proxy-1.1.1.tar.gz
| Download URL | nr.proxy-1.1.1.tar.gz |
|---|---|
| Size | 9.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a633acfad03f9610bfbc53d525764ba997951750c375d30d0df4b7646748ac67
|
|
BLAKE2b-256 checksum How to use checksums |
8a478b303f34ba861107a1f75f79a62bbe7f787592fcccde8f01875539d33e96
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
|
Release files / nr.proxy-1.1.1-py3-none-any.whl
| Download URL | nr.proxy-1.1.1-py3-none-any.whl |
|---|---|
| Size | 8.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3dc2dad6e1ea809440b6fa7d32e9778860f33f48e43f3e7218e8dcdb2c6564b5
|
|
BLAKE2b-256 checksum How to use checksums |
6f9f00c4e9bb9dffd099fbe029f03d44d65627a07d2d20201f64c965c6146df8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
|