Coder Social home page Coder Social logo

eff's Introduction

Eff

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:

  • Easy to understand. You don't need to read long scientific papers to understand how to use the library and what it does.
  • Easy to integrate. If you decided to add a logger into a function, changes will be minimal, no need to pass the logger down through the whole call stack.
  • Fast. The classic approach for handling algebraic effects is using coroutines or exceptions. This library uses global shared state instead which doesn't require to unwrap the whole call stack at runtime.
  • Type-safe. Effect handlers container is just a class. Annotate effect handlers type to make their usage type-safe.

Installation

python3 -m pip install --user eff

Usage

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()

Further reading

You don't have to read it but it's a good reading for better understanding of the motivation behind the library.

eff's People

Contributors

orsinium avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.