In [ ]:
import pandas as pd
import random
In [ ]:
def func1(h: dict):
h['foo']=10
def func2(h: dict):
h['bar']=random.randint(10, 100)
def func3(h: dict):
h['baz']=h['foo']+h['bar']
#apply each function to the product of the previous function.
#Start with the input dictionary
d1 = {'firstElement':'good'}
func1(d1)
func2(d1)
func3(d1)
d1
In [ ]:
def recApply(flist, h=None):
"""
Helper Apply the list of functions iteratively over the dictionary passed
:obj: function list each will be applied to the dictionary sequentially.
"""
#if no dictionary passed, then set the dictionary.
if(h == None):
h = {}
#iteratively call functions with dictionary as a passed parameter and returning a derived dictionary
for f in flist:
h = f(h)
return(h)
In [ ]:
def recApply(flist, h=None):
for f in flist:
f(h)
return h
In [ ]:
flist = [func1,func2,func3]
result = {'firstElement':'good'}
In [ ]:
recApply(flist,result)
result
In [ ]:
result=None
recApply(flist,result)
result
In [ ]:
result={}
recApply(flist,result)
result
In [ ]:
resulta == resultb
In [ ]:
resultb
In [ ]:
series = range(10)
loop_index = 1
result = pd.DataFrame([recApply(flist=flist, h={'iter': loop_index}) for i in series],
index=series)
result
In [ ]: