Skip to main content

pure effects for Python

Project description

Effect

https://travis-ci.org/python-effect/effect.svg?branch=master https://img.shields.io/pypi/v/effect.svg

Effect is a library for helping you write purely functional code by isolating the effects (that is, IO or state manipulation) in your code. Documentation is available at https://effect.readthedocs.org/, and its PyPI page is https://pypi-hypernode.com/pypi/effect.

It supports 3.6 and above.

You can install it by running pip install effect.

https://radix.github.io/effect/sigh-defects.png

What Is It?

Effect lets you isolate your IO and state-manipulation code.

The benefits of this are many: first, the majority of your code can become purely functional, leading to easier testing and ability to reason about behavior. Also, because it separates the specification of an effect from the performance of the effect, there are two more benefits: testing becomes easier still, and it’s easy to provide alternative implementations of effects.

Effect is somewhat similar to “algebraic effects”, as implemented in various typed functional programming languages. It also has similarities to Twisted’s Deferred objects.

Example

A very quick example of using Effects:

from effect import sync_perform, sync_performer, Effect, TypeDispatcher

class ReadLine(object):
    def __init__(self, prompt):
        self.prompt = prompt

def get_user_name():
    return Effect(ReadLine("Enter a candy> "))

@sync_performer
def perform_read_line(dispatcher, readline):
    return raw_input(readline.prompt)

def main():
    effect = get_user_name()
    effect = effect.on(
        success=lambda result: print("I like {} too!".format(result)),
        error=lambda e: print("sorry, there was an error. {}".format(e)))

    dispatcher = TypeDispatcher({ReadLine: perform_read_line})
    sync_perform(dispatcher, effect)

if __name__ == '__main__':
    main()

Effect takes what we call an intent, which is any object. The dispatcher argument to sync_perform must have a performer function for your intent.

This has a number of advantages. First, your unit tests for get_user_name become simpler. You don’t need to mock out or parameterize the raw_input function - you just call get_user_name and assert that it returns a ReadLine object with the correct ‘prompt’ value.

Second, you can implement ReadLine in a number of different ways - it’s possible to override the way an intent is performed to do whatever you want.

For more information on how to implement the actual effect-performing code, and other details, see the documentation. There is also a full example of interacting with the user and using an HTTP client to talk to the GitHub API in the effect-examples repository.

Videos

Some talks have been given about Effect.

Thanks

Thanks to Rackspace for allowing me to work on this project, and having an excellent open source employee contribution policy

Authors

Effect was originally written by Christopher Armstrong, but now has contributions from the following people:

IRC

There is a #python-effect IRC channel on irc.freenode.net.

See Also

For integrating Effect with Twisted’s Deferreds, see the txEffect package (pypi, github).

Over the past few years, the ecosystem of libraries to help with functional programming in Python has exploded. Here are some libraries I recommend:

  • pyrsistent - persistent (optimized immutable) data structures in Python

  • toolz - a general library of pure FP functions

  • fn.py - a Scala-inspired set of tools, including a weird lambda syntax, option type, and monads

License

Effect is licensed under the MIT license:

Copyright (C) 2014 Christopher Armstrong

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

effect-1.1.0.tar.gz (33.7 kB view details)

Uploaded Source

Built Distribution

effect-1.1.0-py2.py3-none-any.whl (41.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file effect-1.1.0.tar.gz.

File metadata

  • Download URL: effect-1.1.0.tar.gz
  • Upload date:
  • Size: 33.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.3

File hashes

Hashes for effect-1.1.0.tar.gz
Algorithm Hash digest
SHA256 7affb603707c648b07b11781ebb793a4b9aee8acf1ac5764c3ed2112adf0c9ea
MD5 87a4408b01acfa6ba51d6b5c5b6b1d11
BLAKE2b-256 317a3c7a4568ed3a8fa463ffabbc70ed9471d82daad4e479b305d299bec72b49

See more details on using hashes here.

Provenance

File details

Details for the file effect-1.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: effect-1.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 41.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.3

File hashes

Hashes for effect-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 01ad6456b806819f9a2aa092c11321f89113028197f2e85170556f8fb87c0f4b
MD5 df2ad530b6f0b7a95eb4dafd022b404e
BLAKE2b-256 5696d1b0c7366a6b3a94ea122f81e381437f769f77672acbe1f71d0a6bdeb7f9

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page