I have a python function having a dataframe and parameters that is function1(df, 'A') or function1(df, 'A, B, C'). and I have 3 other functions FunctionA(df_A), FunctionB(df_B), FunctionC(df_C) df_A, df_B, df_C are arguments for dataframes passed from function1.
I need to call FunctionA if the passed argument in function1 is having value A, similary, FunctionB if the passed argument in function1 is having value B.
If all 3 values (A,B,C) are passed in Function1 then I need to execute the FunctionA then FunctionB then FunctionC in the same order and using the same dataframe. The dataframe returned from FunctionA will become input to FunctionB then input to FunctionC.
If the order of parameters are changed that is, if it (B,A,C) then, function1 will call FunctionB first, then the dataframe returned from FunctionB will become input to FunctionA then input to FunctionC, and finally will have o/p returned from FunctionC.
So, question is there an efficient way to handle it using python all the possible combinations of A,B,C or A,C,B or B,A,C or B,C,A or C,A,B or C,B,A ? I wanted to reduce the number of if statements to handle these cases. And if the number of arguments passed increased to 4 say A,B,C,D then if statements would explode to larger number. Is there a way to handle this scenario in efficient way? Please suggest.
functionA(df_A):
print("inside A")
# just for example I am doing something here
df_A.reset_index(drop=True, inplace=True)
return df_A
functionB(df_B):
print("inside B")
# just for example I am doing something here
df_B.reset_index(drop=True, inplace=True)
return df_B
functionB(df_C):
print("inside C")
# just for example I am doing something here
df_C.reset_index(drop=True, inplace=True)
return df_C
def function1(df, seq_abc):
r = [x.strip().lower() for x in seq_abc.split(',')]
if r[0] == 'A' and r[1] == 'B' and r[2] == 'C':
df = functionA(df)
df = functionB(df)
df = functionC(df)
print("A then B then C executed!")
if r[0] == 'A' and r[1] == 'C' and r[2] == 'B':
df = functionA(df)
df = functionC(df)
df = functionB(df)
print("A then C then B executed!")
if r[0] == 'B' and r[1] == 'C' and r[2] == 'A':
df = functionB(df)
df = functionC(df)
df = functionA(df)
print("B then C then A executed!")
returned_value = function1(df, "A,B,C") # the function1 will call FunctionA or B or C based on parameter passed.
print(returned_value)