Package: effect
EffectEffect 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 athttps://effect.readthedocs.org/, and its PyPI page ishttps://pypi.python.org/pypi/effect.Itsupports3.6 and above.You can install it by runningpip install effect.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.ExampleA very quick example of using Effects:fromeffectimportsync_perform,sync_performer,Effect,TypeDispatcherclassReadLine(object):def__init__(self,prompt):self.prompt=promptdefget_user_name():returnEffect(ReadLine("Enter a candy> "))@sync_performerdefperform_read_line(dispatcher,readline):returnraw_input(readline.prompt)defmain():effect=get_user_name()effect=effect.on(success=lambdaresult:print("I like{}too!".format(result)),error=lambdae:print("sorry, there was an error.{}".format(e)))dispatcher=TypeDispatcher({ReadLine:perform_read_line})sync_perform(dispatcher,effect)if__name__=='__main__':main()Effecttakes what we call anintent, which is any object. Thedispatcherargument tosync_performmust have aperformerfunction
for your intent.This has a number of advantages. First, your unit tests forget_user_namebecome simpler. You don’t need to mock out or parameterize theraw_inputfunction - you just callget_user_nameand 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 thedocumentation. There is also a full example
of interacting with the user and using an HTTP client to talk to the GitHub
API in theeffect-examplesrepository.VideosSome talks have been given about Effect.Side Effects are a Public APIby Chris Armstrong atStrange Loop(2015-09-26)Functionalish Programming in Python with Effectby Robert Collins atKiwi PyCon(2015-09-05)ThanksThanks to Rackspace for allowing me to work on this project, and having anexcellentopen source employee contribution policyAuthorsEffect was originally written byChristopher Armstrong,
but now has contributions from the following people:cylilvhManish TomarTom PrinceIRCThere is a#python-effectIRC channel on irc.freenode.net.See AlsoFor integrating Effect with Twisted’s Deferreds, see thetxEffectpackage
(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 Pythontoolz- a general library of pure FP functionsfn.py- a Scala-inspired set of tools, including a weird lambda syntax, option type, and monadsLicenseEffect is licensed under the MIT license:Copyright (C) 2014 Christopher ArmstrongPermission 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.