Package: python-chain
An easy to use function chaining pattern on Python.📖 About this ProjectChaining functions is a common functional development pattern that is pretty difficult on Python. Usually, we need to pass some data through a pipeline, process or series of functions in order to get a specific output. Without this lib, you would need to wrap those functions on a class or assign each result on a variable.Withpython-chainyou can create an initial state and execute a chain of functions, nourishing that state during the pipeline, like this:🤖 Getting StartedOn this section, you'll learn all the prerequisites and basic knowledge in order to use this library on your projects.InstallationYou can install it usingpip, running:pipinstallpython-chainCommon UsageCreating Chainable FunctionsYou can chain functions by decorating them with the@chaindecorator. Like the following:importchain@chaindefsome_pretty_func(state):...# Now you can chain that function with the operator>>Using StateEvery chain has astate. That state is an Object with immutable attributes. The current Chain state will be passed automatically on the keyword argumentcontext. Every chain should start with a given state, even if it empty. You can create a new one by using:importchainstate=chain.state()If you want to feed data into your initial state, you can pass then as kwargs. The key-value pair on the kwargs of your state will be passed as attributes on your chain context. Like so:importchainstate=chain.state(foo='bar')@chaindeftest_chain(context):print(context.foo)# barUsing States on ChainsEvery mutation that you do in a chain function will add it to the next function. You can merge what you have learned both on states and functions by following:importchain@chaindefcalculate_average(context,type='meter'):nbs=[house.get(type)forhouseinstate.houses]context.avg=sum(nbs)/len(nbs)@chaindefadd_houses(context):houses=[{meter:3,},{meter:10,},]context.houses=housesresult=chain.state()>>add_houses>>calculate_averageprint(result.current)## {#   avg: 6.5,#   houses: [#     { meter: 3 },#     { meter: 10 },#   ]# }#If you don't return anything on your final chain function it will automatically return the Context object. They have a lot of properties, and one of them is thecurrentattribute. That will return the current state of your given context.Finishing a ChainEvery time a chain is finished, it will automatically return its context. You can also add an output by retuning the data that you want on the last step of the chain, like this:importchain@chaindefget_name(context):returncontext.name@chaindefadd_user(context):context.name='foo'result=chain.state()>>add_user>>get_nameprint(result.output)## 'foo'#Passing arguments directlyYou can pass any args or kwargs directly to the next function. They should be passed returning a tuple with all the args on the first argument and the kwargs on the second. You can do so like this:importchain@chaindefstore_result(result,context,type=None):context.result=resultcontext.type=type@chaindefadd_result(context):args=('foo',)kwargs={type:'bar'}returnargs,kwargsresult=chain.state()>>add_result>>store_resultprint(result.current)## {#   result: 'foo',#   type: 'bar',# }#Be careful. This would create astrong dependencybetween those two functions. Chain will always pass the args and kwargs that you've created and it will break the chain if the next function doesn't accept those params. Also, always set a state params, because it will be passed by the Chain with the current state.✍️ ContributingContributions are what makes the open-source community such an amazing place to learn, inspire, and create. Any contributions you make aregreatly appreciated. You can learn how to contribute to this project on theCONTRIBUTINGfile.🔓 LicenseDistributed under the MIT License. SeeLICENSEfor more information.