yalambda
Yalambda lets you write Yanex.Cloud Functions with less boilerplate
Features:
- everything is type-annotated, so you'll get autocompletion in IDEs
- base64 de/encoding and other details are handled for you
- automatically parse JSON using
dataclass-factory
Echo server example
from yalambda import function, YaRequest, YaResponse
@function()
async def handler(req: YaRequest) -> YaResponse:
return YaResponse(200, req.body)
Automatically parse dataclasses
from dataclasses import dataclass
from yalambda import function, YaResponse
@dataclass
class Point:
x: float
y: float
@function()
async def handler(point: Point) -> YaResponse:
dist = (point.x**2 + point.y**2)**0.5
return YaResponse(200, {"distance_to_origin": dist})
Access both the dataclass and the request
from dataclasses import dataclass
from yalambda import function, YaRequest, YaResponse
@dataclass
class Point:
x: float
y: float
@function()
async def handler(point: Point, req: YaRequest) -> YaResponse:
if req.http_method != "POST":
return YaResponse(405, "Only POST requests are allowed")
dist = (point.x**2 + point.y**2)**0.5
return YaResponse(200, {"distance_to_origin": dist})
Initialize something asynchronously on first call
from yalambda import function, YaRequest, YaResponse
async def init():
global answer
answer = 42
@function(init)
async def handler(req: YaRequest) -> YaResponse:
return YaResponse(200, "Answer: {}".format(answer))
Routing
from dataclasses import dataclass
from yalambda import dispatch, YaRequest, YaResponse
@dataclass
class Point:
x: float
y: float
async def get_all_points(req: YaRequest) -> YaResponse:
points = [{"x": 3.0, "y": 4.0}, {"x": -1.0, "y": 3.27}]
return YaResponse(200, points)
async def compute_distance(point: Point) -> YaResponse:
dist = (point.x**2 + point.y**2)**0.5
return YaResponse(200, {"distance_to_origin": dist})
handler = dispatch({
"GET": get_all_points,
"POST": compute_distance,
})
Full example
This function acts as a GitHub webhook and sends a pretty embed on Discord webhook when an issue is opened or closed. See the source code on GitHub.
Development server
You can install aiohttp and run your function locally.
It's not the same as the real thing, but it should be enough for simple functions.
$ python -m yalambda your_module
======== Running on http://0.0.0.0:55710 ========
(Press CTRL+C to quit)
Condition DSL
We can modify our GitHub->Discord example so that it doesn't error out on the initial ping event:
from yalambda import when
...
async def handle_issue_events(event: IssueEvent) -> YaResponse:
embed = create_embed(event)
if embed is not None:
await client.post(DISCORD_WEBHOOK, json={"embeds": [embed]})
return YaResponse(200, "")
async def handle_ping(req: YaRequest) -> YaResponse:
return YaResponse(200, "")
handler = when.dispatch(
when.header_is("x-github-event", "ping", handle_ping),
when.header_is("x-github-event", "issues", handle_issue_events),
init=init
)
Metadata
Release files for yalambda 0.6.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| yalambda-0.6.0.tar.gz | 8.8 kB | Without provenance | Details for yalambda-0.6.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| yalambda-0.6.0-py3-none-any.whl | Python 3 | none | any | Details for yalambda-0.6.0-py3-none-any.whl |
Total release size: 17.8 kB
Release files / yalambda-0.6.0.tar.gz
| Download URL | yalambda-0.6.0.tar.gz |
|---|---|
| Size | 8.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f3c9bff6e5eb2a0d52bf1e8202b9f8f2d43ad87f2a61667604f603e502387eb9
|
|
BLAKE2b-256 checksum How to use checksums |
f60cfb080651a2cdf033ecb05fa7366b53a0c4769a19ba28fb2b75df254f47d8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.8 CPython/3.8.8 Linux/5.10.63-1-MANJARO
|
Release files / yalambda-0.6.0-py3-none-any.whl
| Download URL | yalambda-0.6.0-py3-none-any.whl |
|---|---|
| Size | 8.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
94c01b4d0e58cc8e7c41986f9b28c3dcf78fac5f41b0d79f4efb0692fb9cf5fc
|
|
BLAKE2b-256 checksum How to use checksums |
ae6c7654c4f099e8b1895ae3e3e209f80c0dc62bffce1e6f760679ee4e7b3498
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.8 CPython/3.8.8 Linux/5.10.63-1-MANJARO
|
Security for yalambda 0.6.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.