ProxyCTX is a utility library for global context inspired by Flask, leveraging contextvars
Project description
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
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 proxyctx-0.1.1.tar.gz
.
File metadata
- Download URL: proxyctx-0.1.1.tar.gz
- Upload date:
- Size: 19.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.1 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6aa10de69898bb0f3f61f9f2c76fd38f9196d1d183b656d48e9a24b475914b5 |
|
MD5 | 4041ae8539f0ea21c8e7cebba4b034ed |
|
BLAKE2b-256 | 5587582fd2f35841a2d445aa7299d3ce44b36949bb0bddcd9ca7c1ad33df1c3e |
File details
Details for the file proxyctx-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: proxyctx-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.1 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3d998e781912b9f9773c0464036090be959c84986cf9d2947e2ca6fbd48dcb0 |
|
MD5 | be03da02ec948e042077d564582efd9b |
|
BLAKE2b-256 | 037499b97cacce35467ac32bc3804bb8e0112558fe4edc1ad79fbcb8ad7f41b1 |