Home > Blockchain >  Python ignoring my last 2 elif statements but reading the first 2?
Python ignoring my last 2 elif statements but reading the first 2?

Time:01-07

I have my code here and I don't know why python keeps ignoring my elif statements:

def findDiff(dbDict, s3dict,scenario):
    comparison={}
    dtype_table_check=True
    if  scenario=="DTYPE_COMPARISON":
        comparison["DTYPE_COMPARISON"]={}
        for k in dbDict:
            comparison["DTYPE_COMPARISON"][k]={}
            if k in s3dict:
                if dbDict==s3dict:
                    comparison["DTYPE_COMPARISON"]["ALL_DATATYPES"]={}
                    comparison["DTYPE_COMPARISON"]["ALL_DATATYPES"]["RESULT"]=True
                    break
                else:
                    for ke in dbDict[k]:
                        try:
                            x= s3dict[k][ke]
                            if (dbDict[k][ke]==s3dict[k][ke]):
                                comparison["DTYPE_COMPARISON"][k][ke]={}
                                comparison["DTYPE_COMPARISON"][k][ke]["RESULT"]=True
                            else:
                                comparison["DTYPE_COMPARISON"][k][ke]={}
                                comparison["DTYPE_COMPARISON"][k][ke]["RESULT"]=False
                                dtype_table_check=False
                        except KeyError:
                            comparison["DTYPE_COMPARISON"][k][ke]={}
                            comparison["DTYPE_COMPARISON"][k][ke]["RESULT"]=False
                            dtype_table_check=False
            else:
                comparison["DTYPE_COMPARISON"]["ALL_DATATYPES"]["RESULT"]=True
        comparison["DTYPE_COMPARISON"][k]["RESULT"]=dtype_table_check

    elif scenario=="TOTAL_COUNT_COMPARISON":
        comparison["TOTAL_COUNT_COMPARISON"]={}
        for k in dbDict:
            if k in s3dict:
                if dbDict==s3dict:
                    comparison["TOTAL_COUNT_COMPARISON"][k]={}
                    comparison["TOTAL_COUNT_COMPARISON"][k]["RESULT"]=True
                    break
                else:
                    #print("db: " str(dbDict[k]))
                    #print("s3: " str(s3dict[k]))
                    comparison["TOTAL_COUNT_COMPARISON"][k]={}
                    if (dbDict[k]==s3dict[k]):
                        comparison["TOTAL_COUNT_COMPARISON"][k]["RESULT"]=True
                    else:
                        comparison["TOTAL_COUNT_COMPARISON"][k]["RESULT"]=False
            else:
                comparison["TOTAL_COUNT_COMPARISON"][k]["RESULT"]=False
    
    
    elif "NULL_COUNT_COMPARISON":
        comparison["NULL_COUNT_COMPARISON"]={}
        for k in dbDict:
            if k in s3dict:
                if dbDict==s3dict:
                    comparison["NULL_COUNT_COMPARISON"][k]={}
                    comparison["NULL_COUNT_COMPARISON"][k]["RESULT"]=True
                    break
                else:
                    comparison["NULL_COUNT_COMPARISON"][k]={}
                    if (dbDict[k]==s3dict[k]):
                        comparison["NULL_COUNT_COMPARISON"][k]["RESULT"]=True
                    else:
                        comparison["NULL_COUNT_COMPARISON"][k]["RESULT"]=False
            else:
                comparison["NULL_COUNT_COMPARISON"][k]["RESULT"]=False
    
    
    elif "UNIQUE_COUNT_COMPARISON":
                comparison["UNIQUE_COUNT_COMPARISON"]={}
                for k in dbDict:
                    if k in s3dict:
                        if dbDict==s3dict:
                            comparison["UNIQUE_COUNT_COMPARISON"][k]={}
                            comparison["UNIQUE_COUNT_COMPARISON"][k]["RESULT"]=True
                            break
                        else:
                            comparison["UNIQUE_COUNT_COMPARISON"][k]={}
                            if (dbDict[k]==s3dict[k]):
                                comparison["UNIQUE_COUNT_COMPARISON"][k]["RESULT"]=True
                            else:
                                comparison["UNIQUE_COUNT_COMPARISON"][k]["RESULT"]=False
                    else:
                        comparison["UNIQUE_COUNT_COMPARISON"][k]["RESULT"]=False
    
    elif "UNIQUE_COMBINATION_COUNT_COMPARISON":
                comparison["UNIQUE_COMBINATION_COUNT_COMPARISON"]={}
                for k in dbDict:
                    if k in s3dict:
                        if dbDict==s3dict:
                            comparison["UNIQUE_COMBINATION_COUNT_COMPARISON"][k]={}
                            comparison["UNIQUE_COMBINATION_COUNT_COMPARISON"][k]["RESULT"]=True
                            break
                        else:
                            comparison["UNIQUE_COMBINATION_COUNT_COMPARISON"][k]={}
                        if (dbDict[k]==s3dict[k]):
                            comparison["UNIQUE_COMBINATION_COUNT_COMPARISON"][k]["RESULT"]=True
                        else:
                            comparison["UNIQUE_COMBINATION_COUNT_COMPARISON"][k]["RESULT"]=False
                else:
                    comparison["UNIQUE_COMBINATION_COUNT_COMPARISON"][k]["RESULT"]=False


    elif "DATAVALUE_COMPARISON":
        print("DATAVALUE_COMPARISON: ")
            #get table

    return comparison
    

When I move the elif statement for "UNIQUE_COUNT_COMPARISON" above "NULL_COUNT_COMPARISON" it works, but then it can't read "NULL_COUNT_COMPARISON" or anything below it. And same goes for the other elif statements. "TOTAL_COUNT_COMPARISON" and "NULL_COUNT_COMPARISON" work as is, and "UNIQUE_COUNT_COMPARISON" and "UNIQUE_COMBINATION_COUNT_COMPARISON" and "DATAVALUE_COMPARISON" are ignored. If I move "UNIQUE_COMBINATION_COUNT_COMPARISON" above "NULL_COUNT_COMPARISON" then it reads the "UNIQUE_COMBINATION_COUNT_COMPARISON" and "TOTAL_COUNT_COMPARISON" but ignored "NULL_COUNT_COMPARISON", "UNIQUE_COUNT_COMPARISON", and "DATAVALUE_COMPARISON".

I am declaring these strings for use later in my code and it can't "find it" because they aren't being read in the first place.

CodePudding user response:

elif "NULL_COUNT_COMPARISON":

is equivalent to:

elif True:

because non-empty strings are "truthy". Python doesn't infer that what you want to do is compare that string to scenario like you did in the prior cases -- it just evaluates the expression you gave it, which in this case is a non-empty string, not an equality check.

If the if/elif chain makes it that far, that will be the block that gets evaluated. No elif/else following that can possibly be evaluated.

CodePudding user response:

The following line is evaluated as True, as any non-empty string in Python:

elif "NULL_COUNT_COMPARISON"

You need to compare the string with something like == or !=.

  • Related