proxyctx
ProxyCTX is a handy utility library that empowers developers to establish a global context accessible from anywhere within their application. Its creation was inspired by Flask, a popular web framework. Under the hood, ProxyCTX leverages the Python standard module contextvars.
The library consists of just two key components:
Proxy: Inspired by Werkzeug's LocalProxy class (werkzeug.local)Context: Inspired by Flask's AppContext class (flask.ctx)
Flask Example
In Flask, when defining a route, the corresponding method does not receive any parameters representing the incoming request. Instead, you can import a global variable called request, which conveniently holds information about the incoming request. Here's an example code snippet illustrating this:
from flask import Flask, request
app = Flask(__name__)
@app.route("/")
def index():
print(request.method)
It's worth noting that request is not the only global variable available in Flask. You can explore the flask.globals module, which encompasses all the global variables provided by Flask.
Usage
To install the library use the following command:
pip install proxyctx
Proxy
from contextvars import ContextVar
from proxyctx import Proxy
class Greet:
def __init__(self, message: str = None):
self.message = message
def greet(self, name: str):
if self.message is None:
print(f"Hello {name} !")
else:
print(f"Hello {name}, {self.message}")
ctx: ContextVar["Greet"] = ContextVar("ctx")
current_greet: "Greet" = Proxy(ctx)
current_message: str = Proxy(ctx, lambda obj: obj.message)
ctx.set(Greet("how are you ?"))
print(current_message) # "how are you ?"
current_greet.greet("Lucino772") # "Hello Lucino772, how are you ?"
ctx.set(Greet("have a nice day !"))
print(current_message) # "have a nice day !"
current_greet.greet("Lucino772") # "Hello Lucino772, have a nice day !"
Context
from contextvars import ContextVar
from proxyctx import Proxy, Context
ctx: ContextVar["GreetContext"] = ContextVar("ctx")
current_greet: "Greet" = Proxy(ctx, lambda obj: obj.greet)
class Greet:
def __init__(self, message: str = None):
self.message = message
def greet(self, name: str):
if self.message is None:
print(f"Hello {name} !")
else:
print(f"Hello {name}, {self.message}")
def greet_context(self):
return GreetContext(self)
class GreetContext(Context):
def __init__(self, greet: "Greet"):
super().__init__(ctx)
self.greet = greet
with Greet("how are you ?").greet_context():
current_greet.greet("Lucino772") # "Hello Lucino772, how are you ?"
with Greet("have a nice day !").greet_context():
current_greet.greet("Lucino772") # "Hello Lucino772, have a nice day !"
current_greet.greet("Lucino772") # "Hello Lucino772, how are you ?"
Licence
This project uses a MIT Licence view
Metadata
Release files for proxyctx 0.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 | |
|---|---|---|---|
| proxyctx-0.1.1.tar.gz | 19.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| proxyctx-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 26.0 kB
Release files / proxyctx-0.1.1.tar.gz
| Download URL | proxyctx-0.1.1.tar.gz |
|---|---|
| Size | 19.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f6aa10de69898bb0f3f61f9f2c76fd38f9196d1d183b656d48e9a24b475914b5
|
|
BLAKE2b-256 checksum How to use checksums |
5587582fd2f35841a2d445aa7299d3ce44b36949bb0bddcd9ca7c1ad33df1c3e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.1 CPython/3.11.4
|
Release files / proxyctx-0.1.1-py3-none-any.whl
| Download URL | proxyctx-0.1.1-py3-none-any.whl |
|---|---|
| Size | 6.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c3d998e781912b9f9773c0464036090be959c84986cf9d2947e2ca6fbd48dcb0
|
|
BLAKE2b-256 checksum How to use checksums |
037499b97cacce35467ac32bc3804bb8e0112558fe4edc1ad79fbcb8ad7f41b1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.1 CPython/3.11.4
|