Python library to work with algebraic effects
Eff is a Python library to work with algebraic effects.
Algbraic effects are all side-effects of a piece of code, like reading a user input, writing a text on the screen, doing network requests, reading a file etc. Eff allows to easily handle such effects making the code cleaner and testing easier.
Features:
python3 -m pip install --user eff
from io import StringIO
from typing import Callable
import eff
class Eff(eff.ects):
show: Callable
# Use global effects manager in a function.
def greet(username: str) -> None:
Eff.show(f'Hello, {username}!')
# Provide actual side-effects handlers
# for the project prod entry point.
def main() -> None:
with Eff(show=print):
greet('World')
# Mock side-effects in tests.
def test_greet() -> None:
stream = StringIO()
with Eff(show=stream.write):
greet('World')
stream.seek(0)
assert stream.read() == 'Hello, World!'
if __name__ == '__main__':
main()
You don't have to read it but it's a good reading for better understanding of the motivation behind the library.