rdr-py / code / addChosenRule.py
addChosenRule.py
Raw
"""
*******************************************************
Owner: Ashutosh Jha
For: APPCAIR BITS Pilani Goa Campus, Reflexis Systems
Function Name: addChosenRule
This function takes as input a node of class Tree() and
a list containing the column name, value of one row and
another string describing whether the column is of type
str or not.
*******************************************************
"""
def addChosenRule(node,inp_cond_list):
    if(inp_cond_list[2] == "str"):
        print("Do you want substring comparison?(y or n)")
        inp = input()
        if((inp=="y") or (inp=="Y")):
            print("The value of the string column selected by you and it's index are as follows:")
            print("String:",end=" ")
            for i in range(0,len(inp_cond_list[1])):
                print(inp_cond_list[1][i],end=" ")
            print("\nindex: ",end=" ")
            for i in range(0,len(inp_cond_list[1])):
                print(i,end=" ")
            print("\n")
            print("Insert the starting and ending index of the substring " +
                    "that you want to compare.(starting from 0)")
            print("Starting index:",end=" ")
            s_ind=int(input())
            print("Ending index:",end=" ")
            e_ind = int(input())
            if((s_ind <= e_ind) and (s_ind >= 0) and (e_ind >= 0)):
                str_to_comp = inp_cond_list[1][s_ind:e_ind+1]
                node.inp_cond = "cur_row['" + inp_cond_list[0] + "'][" + str(s_ind) + ":" + str(e_ind+1) + "]=='" + str_to_comp + "'"
            else:
                print("Invalid Indexes: starting",s_ind,"and ending:",e_ind)
                node.inp_cond = None
        else:
            node.inp_cond = "cur_row['" + inp_cond_list[0] + "']==" + "'" + inp_cond_list[1] + "'"
        #Only euality that is == operator is supported for rules involving strings
    else:
        op_dict = {1:"==",2:"<=",3:">="}
        print("Kindly choose the operator by indicating" + 
              " the serial number from the following:")
        for key, value in op_dict.items():
            print(key, ' : ', value)
        op = 0
        while ( op not in [1,2,3]):
            op = int(input())
            if op in [1,2,3]:
                node.inp_cond = "cur_row['" + inp_cond_list[0] + "']" + op_dict[op] + inp_cond_list[1]
                break
            else:
                print("Fail: Wrong input for operator. Enter Again.")
                continue
    
    node.data ="""
if(eval(node.inp_cond) == True):
    node.belongs = True
    print("Row No.",z,"satisfies condition in node",node.name)
else:
    print("Row No.",z,"Does not satisfy condition in node",node.name)
    node.belongs = False"""