Algebraic Effect for modern Python.
Project description
Affection
Algebraic Effect for modern Python.
Tips: I strongly recommend you to read Algebraic Effects for the Rest of Us before you start.
Affection
is a small library that implements Algebraic Effect
for Python.
It leverages asyncio
, decorator and with
syntax for a smooth experience.
Warning: Please note that until Higher Rank Type Variants
are supported in Python, the Effect.handle
API will be quite ugly.
Usage
This project is designed to be a single file project.
You can either directly copy it into your project, or add affection
from PyPI
with your favorite package manager.
from affection import Effect, Handle, effect, perform
class Log(Effect[None]):
def __init__(self, content: str):
self.content = content
def get_name(name: str | None = None) -> str:
perform(Log(f"Getting name with {name}"))
return perform(effect("ask_name", str)) if name is None else name
def main():
with Handle() as h:
@Log.handle(h)
def _(l: Log):
print(l.content)
@effect("ask_name", str).handle(h)
def _(_) -> str:
return "Default"
perform(Log("Test parent log"))
with Handle() as i_h:
@Log.handle(h)
def _(l: Log):
print("Inner", l.content.lower())
print(get_name("Ann"))
print(get_name())
if __name__ == "__main__":
main()
"""
Test parent log
Inner getting name with ann
Ann
Inner getting name with none
Default
"""
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 affection-0.1.0.tar.gz
.
File metadata
- Download URL: affection-0.1.0.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.0.1 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17fa4a048d8dc04ecd5e9d855a56ef02a63a5f2e65e33d33342b22ecea32bad3 |
|
MD5 | a623805f0c230f95f1559fa211b0fe18 |
|
BLAKE2b-256 | c5bb3dfd3024bb7d167f7eeb72bdc9ac6348f5dd5037398c9d4e74aa68ca7f00 |
File details
Details for the file affection-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: affection-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.0.1 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4038ded22fc2ab164bb5c54e60fbada5bf2213ecdadbaedcb5ecfd1d0e1cffb6 |
|
MD5 | cb4becc647308628928705033497fb2d |
|
BLAKE2b-256 | 76031b0cf8fa7c24a1c0691e27580cc3e84fbe69dea43046dc87aa2d471eb71a |