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
affection-0.1.1.tar.gz
(3.6 kB
view hashes)
Built Distribution
Close
Hashes for affection-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 926cbaf37b861dd67c261defd52c378d845d0544e8f19e6decbe3ab5bd85c19e |
|
MD5 | c563b6d3c83e762b80b27ef48de55d88 |
|
BLAKE2b-256 | 324396733f5b44c1343b2d12c9d5d1cfd83f1d2ba267af017412c8f5caed228b |