Skip to main content

pure effects for Python

Project description

Effect

https://travis-ci.org/radix/effect.svg?branch=master

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/.

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

Status: Alpha

Right now Effect is in alpha, and is likely to change incompatibly. Once it’s being used in production and the API seems pretty good, a final version will be released. Because it’s in alpha, pip requires that you explicitly specify the version number when specifying a dependency. This means that you won’t automatically get upgraded to newer versions that potentially break the API. e.g., use pip install effect==0.1aN.

What Is It?

Effect lets you isolate your IO and state-manipulation code, by using a system very similar to Haskell’s extensible-effects package. It also has similarities to Twisted’s Deferred objects.

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.

Example

A very quick example of using Effects:

from __future__ import print_function
from effect import 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})
    perform(dispatcher, effect)

if __name__ == '__main__':
    main()

Effect takes what we call an intent, which is any object. The dispatcher argument to 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 examples directory.

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

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-0.1a17.tar.gz (25.6 kB view details)

Uploaded Source

Built Distribution

effect-0.1a17-py2.py3-none-any.whl (42.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file effect-0.1a17.tar.gz.

File metadata

  • Download URL: effect-0.1a17.tar.gz
  • Upload date:
  • Size: 25.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for effect-0.1a17.tar.gz
Algorithm Hash digest
SHA256 9c6e64daec3fb6e4d3beddc7b080b21abc87fc66e5037177afd389753a650d8b
MD5 1b0fc08c8d3bedfbd6228fbb94b95980
BLAKE2b-256 6faa97d5053b553b5b384122b75251df3b9ee5639c4d5d5d6b490b015334c024

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for effect-0.1a17-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c07c94b0d537040b797c5502f9797f5887fd4b7d3a7572ae7696b56ed96e9b56
MD5 2c40f26eecef231c9bc4e32a6f46daa7
BLAKE2b-256 de8d16c499f65b6bcf55a713169f2a5dc12825d86e6a4bf92a12706f6826a30d

See more details on using hashes here.

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