rdr-py / code / addCombination.py
addCombination.py
Raw
"""
*******************************************************
Owner: Ashutosh Jha
For: APPCAIR BITS Pilani Goa Campus, Reflexis Systems
Function Name: addCombination
This function takes column list, current row and target
column as input. It asks the user if they want to use 
more than one column for a rule, if yes it takes the 
expression as input and returns a list containing the 
rule, another list containing features used and a str
value indcating expression involving strings.
*******************************************************
"""
def addCombination(col_list,cur_row,target_col):
    wantComb = input("Do you want to enter a condition using more than one column?(y or n)")
    if((wantComb != "y") and (wantComb != "Y")):
        return None,None,None
    else:
        print("This program only supports(+,-,*,/), however make sure no" +
             " NaN values are present in the column which is divisor.\n" + 
             "If operation involves string, only '+' is supproted," +
             "all columns must be string type.\n" +
             "Use brackets to specify equation along with column names.")
        isstr = input("Is the operation you wish to do deal with string columns only?(y or n)")
        print("If column list = [col1,col2,col3,col4,col5] then one possible expression for LHS is:")
        print("((col1+col2)-(col3/col5)*col4), kindly provide all brackets.")
        print("Enter the LHS expression using Column List given below:")
        for i in range(0,len(col_list)):
            if(col_list[i] != target_col):
                print(col_list[i],"(",type(cur_row[col_list[i]]),")")
        print("\n")
        exp = input()
        exp = [l for m in exp.split(sep='(') for l in (m, '(')][:-1] 

        feature_used =[]
        i=0
        #The following while loops split the expression into brackets, operators and operands
        #and stores then in a list.
        while(i<len(exp)):
            if((exp[i]!="(") and (exp[i]!="") and (exp[i]!=")")):
                nw = [l for m in exp[i].split(sep=')') for l in (m, ')')][:-1] 
                exp.pop(i)
                for z in range(i,i+len(nw)):
                    exp.insert(z,nw[z-i])
                i=i+len(nw)
            else:
                i=i+1
            #print(i,len(exp),exp)
        i=0
        while(i<len(exp)):
            if((exp[i]!="(") and (exp[i]!="") and (exp[i]!=")") and (exp[i]!="/")):
                nw = [l for m in exp[i].split(sep='/') for l in (m, '/')][:-1] 
                exp.pop(i)
                for z in range(i,i+len(nw)):
                    exp.insert(z,nw[z-i])
                i=i+len(nw)
            else:
                i=i+1
            #print(i,len(exp),exp)
        i=0
        while(i<len(exp)):
            if((exp[i]!="(") and (exp[i]!="") and (exp[i]!=")") and (exp[i]!="/")
              and (exp[i]!="*")):
                nw = [l for m in exp[i].split(sep='*') for l in (m, '*')][:-1] 
                exp.pop(i)
                for z in range(i,i+len(nw)):
                    exp.insert(z,nw[z-i])
                i=i+len(nw)
            else:
                i=i+1
            #print(i,len(exp),exp)
        i=0
        while(i<len(exp)):
            if((exp[i]!="(") and (exp[i]!="") and (exp[i]!=")") and (exp[i]!="/")
              and (exp[i]!="*") and (exp[i]!="+")):
                nw = [l for m in exp[i].split(sep='+') for l in (m, '+')][:-1] 
                exp.pop(i)
                for z in range(i,i+len(nw)):
                    exp.insert(z,nw[z-i])
                i=i+len(nw)
            else:
                i=i+1
            #print(i,len(exp),exp)
        i=0
        while(i<len(exp)):
            if((exp[i]!="(") and (exp[i]!="") and (exp[i]!=")") and (exp[i]!="/")
              and (exp[i]!="*") and (exp[i]!="+") and (exp[i]!="-")):
                nw = [l for m in exp[i].split(sep='-') for l in (m, '-')][:-1] 
                exp.pop(i)
                for z in range(i,i+len(nw)):
                    exp.insert(z,nw[z-i])
                i=i+len(nw)
            else:
                i=i+1
            #print(i,len(exp),exp)
        #The below line removes all empty entries from the list of orerators,operands and divisors
        exp = list(filter(lambda a: a != '', exp))
        #the following conditions check the validity of the expression that is entered
        for i in range(0,len(exp)):
            exp[i] = exp[i].strip()
            if((isstr == "y") or (isstr == "Y")):
                if exp[i] in ["-","*","/"]:
                    print(exp[i],"operaton not supported with strings.")
                    return None,None,None
            if((exp[i]!="(") and (exp[i]!=")") and (exp[i]!="/")
              and (exp[i]!="*") and (exp[i]!="+") and (exp[i]!="-")):
                if exp[i] not in col_list:
                    print(exp[i],"is not a valid column name.")
                    return None,None,None
                else:
                    if((isstr == "y") or (isstr == "Y")):
                        if(type(cur_row[exp[i]]) != str):
                            print(exp[i],"is not a string type column, while you specified" +
                                 " earlier that the operation only involves strings.")
                            return None,None,None
                    feature_used.append(exp[i])
        return exp,feature_used,isstr