mardi 9 avril 2019

Python : control does not execute any conditiional statement

Here's my code which is full of conditional statements. Unfortunately none of them gets executed even once. I printed values of all of the conditional variables before the if-elif-else ladder starts. Before each iteration all the condition satisfiy, but the print statements immediately after conditions like print('I am in Match 3') never gets executed. I failed to debug my code.

#finding similarities between products comparing brands, UoMs and unit prices 

def similar(a, b):
    return SequenceMatcher(None, a, b).ratio()

count = 0
match = 0
#prices = []
price_final = []
item_name = []
brands_smer = []
brands_cust = []
messages = []

for j, (x, m) in enumerate(zip(cust_counts, cust_brands)):

    prices100 = []
    prices101 = []
    prices102 = []
    prices103 = []
    prices1 = []
    prices2 = []
    prices3 = []
    prices4 = []
    prices5 = []
    prices6 = []
    prices7 = []
    prices8 = []

    brand100 = []
    brand101 = []
    brand102 = []
    brand103 = []
    brand2 = []
    brand3 = []
    brand4 = []
    brand5 = []
    brand6 = []
    brand7 = []
    brand8 = []

    '''grocery_uom = grocery.loc[j, 'UOM']
    #sep = 
    unit_weight = re.split('[a-zA-Z]+', grocery_uom)[0]
    if unit_weight == ' ':
        unit_weight = 1
    uom = re.split('[a-zA-Z]+', grocery_uom)[1]'''

    uom_cust = grocery.loc[j, 'UOM']
    unit_weight_cust = grocery.loc[j, 'QUANTITY']
    #unit_weight_cust, uom_cust = split_string(uom_grocery)

    #print('unit_weight_cust %s' % unit_weight_cust)
    #print('uom_cust %s' % uom_cust)

    for i, (y, n) in enumerate(zip(smer_counts, smer_brands)):
        similarity = cosine_similarity(x, y)
        brand_similarity = cosine_similarity(m,n)  

        #print('name similarity %f' % similarity[0][0])
        #print('brand similarity %f' % brand_similarity[0][0])

        #print(grocery.loc[j, 'ITEM NAME'])
        #print(tech.loc[i, 'Product Name'])

        #print(grocery.loc[j, 'BRAND'])
        #print(tech.loc[i, 'Brand'])

        #print('-'*80)

        #computes UoMs and quantities of products from customer's specification
        '''uom_grocery = grocery.loc[j, 'UOM']
        unit_weight_cust, uom_cust = split_string(uom_cust)'''

        uom_smer = tech.loc[i, 'UOM']
        unit_weight_smer = tech.loc[i, 'Unit Weight']


        #print('uom_smer %s' % uom_smer)
        #print('unit_weight_smer %s' % unit_weight_smer)

        #print(uom_cust)
        #print(unit_weight_cust)

        #to handle 'KG' and 'KGS' like situations and some more like lowercase and uppercase of characters
        #print('-'*80)
        #print(uom_cust)
        #print(uom_smer)
        #print(tech.loc[i, 'Product Name'])
        #print('-'*80)

        try:
            uom_ratio = similar(uom_cust.lower(), uom_smer.lower())
            print('uom_ratio %f' % uom_ratio)

            print('name similarity %f' % similarity[0][0])
            print('brand similarity %f' % brand_similarity[0][0])

            print(grocery.loc[j, 'ITEM NAME'])
            print(tech.loc[i, 'Product Name'])

            print(grocery.loc[j, 'BRAND'])
            print(tech.loc[i, 'Brand'])

            print('unit_weight_cust %s' % unit_weight_cust)
            print('uom_cust %s' % uom_cust)

            print('uom_smer %s' % uom_smer)
            print('unit_weight_smer %s' % unit_weight_smer)           

            print('-'*80)

            if similarity[0][0]>=0.75 and str(grocery.loc[j, 'BRAND'])=='nan' and uom_ratio>=0.75 and \
            unit_weight_cust == unit_weight_smer :

                print('I am in Match 100')
                print (tech.loc[i, 'Product lower']+"------"+grocery.loc[j, 'Item lower'])
                print (str(tech.loc[i, 'Brand'])+"------"+str(grocery.loc[j, 'BRAND']))
                price = tech.loc[i, 'SKT- Unit Price']
                print(price)

                prices100.append(price)
                item = grocery.loc[j, 'ITEM NAME']
                brand_cust = grocery.loc[j, 'BRAND']
                brand100.append(tech.loc[i, 'Brand'])
                #message.append('Brand, UoM matched, Unit Weight didnt match')
                match = 100

            elif match != 100 and similarity[0][0]>=0.75 and str(grocery.loc[j, 'BRAND'])=='nan' and \
            uom_ratio>=0.75 and unit_weight_cust != unit_weight_smer :

                print('I am in Match 101')
                print (tech.loc[i, 'Product lower']+"------"+grocery.loc[j, 'Item lower'])
                print (str(tech.loc[i, 'Brand'])+"------"+str(grocery.loc[j, 'BRAND']))
                price = tech.loc[i, 'SKT- Unit Price']
                print(price)

                prices101.append(price)
                item = grocery.loc[j, 'ITEM NAME']
                brand_cust = grocery.loc[j, 'BRAND']
                brand101.append(tech.loc[i, 'Brand'])
                #message.append('Brand, UoM matched, Unit Weight didnt match')
                match = 101   

            elif match != 100 and match != 101 and similarity[0][0]>=0.75 and str(grocery.loc[j, 'BRAND'])=='nan' \
            and uom_ratio<0.75 and unit_weight_cust == unit_weight_smer :

                print('I am in Match 102')
                print (tech.loc[i, 'Product lower']+"------"+grocery.loc[j, 'Item lower'])
                print (str(tech.loc[i, 'Brand'])+"------"+str(grocery.loc[j, 'BRAND']))
                price = tech.loc[i, 'SKT- Unit Price']
                print(price)

                prices102.append(price)
                item = grocery.loc[j, 'ITEM NAME']
                brand_cust = grocery.loc[j, 'BRAND']
                brand102.append(tech.loc[i, 'Brand'])
                #message.append('Brand, UoM matched, Unit Weight didnt match')
                match = 102   

            elif match != 100 and match != 101 and match != 102 and similarity[0][0]>0.75 and \
            str(grocery.loc[j, 'BRAND'])=='nan' and \
            uom_ratio<0.75 and unit_weight_cust != unit_weight_smer :

                print('I am in Match 103')
                print (tech.loc[i, 'Product lower']+"------"+grocery.loc[j, 'Item lower'])
                print (str(tech.loc[i, 'Brand'])+"------"+str(grocery.loc[j, 'BRAND']))
                price = tech.loc[i, 'SKT- Unit Price']
                print(price)

                prices103.append(price)
                item = grocery.loc[j, 'ITEM NAME']
                brand_cust = grocery.loc[j, 'BRAND']
                brand103.append(tech.loc[i, 'Brand'])
                #message.append('Brand, UoM matched, Unit Weight didnt match')
                match = 103     

            elif similarity[0][0]>=0.75 and brand_similarity[0][0]>=0.75 and uom_ratio>=0.75 and \
            unit_weight_cust == unit_weight_smer: 

                print('I am in Match 1')
                print (tech.loc[i, 'Product lower']+"------"+grocery.loc[j, 'Item lower'])
                print (str(tech.loc[i, 'Brand'])+"------"+str(grocery.loc[j, 'BRAND']))
                price = tech.loc[i, 'SKT- Unit Price']
                print(price)

                prices1.append(price)
                item = grocery.loc[j, 'ITEM NAME']
                brand_cust = grocery.loc[j, 'BRAND']
                brand_smer = tech.loc[i, 'Brand']
                #message.append('Brand, UoM, Unit Weight matched')
                match = 1

            elif match != 1 and similarity[0][0]>=0.75 and \
            brand_similarity[0][0]>=0.75 \
            and uom_ratio>=0.75 and unit_weight_cust != unit_weight_smer:

                print('I am in Match 2')
                print (tech.loc[i, 'Product lower']+"------"+grocery.loc[j, 'Item lower'])
                print (str(tech.loc[i, 'Brand'])+"------"+str(grocery.loc[j, 'BRAND']))
                price = tech.loc[i, 'SKT- Unit Price']
                print(price)

                prices2.append(price)
                item = grocery.loc[j, 'ITEM NAME']
                brand_cust = grocery.loc[j, 'BRAND']
                brand2.append(tech.loc[i, 'Brand'])
                #message.append('Brand, UoM matched, Unit Weight didnt match')
                match = 2

            elif match != 1 and match != 2 and similarity[0][0]>=0.75 and \
            brand_similarity[0][0]>=0.75 \
            and uom_ratio<0.75 and unit_weight_cust == unit_weight_smer: 

                print('I am in Match 3')
                print (tech.loc[i, 'Product lower']+"------"+grocery.loc[j, 'Item lower'])
                print (str(tech.loc[i, 'Brand'])+"------"+str(grocery.loc[j, 'BRAND']))
                price = tech.loc[i, 'SKT- Unit Price']
                print(price)

                prices3.append(price)
                item = grocery.loc[j, 'ITEM NAME']
                brand_cust = grocery.loc[j, 'BRAND']
                brand3.append(tech.loc[i, 'Brand'])
                #message.append('Brand, Unit Weight matched, UoM didnt match')
                match = 3

            elif match != 1 and match != 2 and match != 3 and similarity[0][0]>=0.75 and \
            brand_similarity[0][0]>=0.75 \
            and uom_ratio<0.75 and unit_weight_cust != unit_weight_smer: 

                print('I am in Match 4')
                print (tech.loc[i, 'Product lower']+"------"+grocery.loc[j, 'Item lower'])
                print (str(tech.loc[i, 'Brand'])+"------"+str(grocery.loc[j, 'BRAND']))
                price = tech.loc[i, 'SKT- Unit Price']
                print(price)

                prices4.append(price)
                item = grocery.loc[j, 'ITEM NAME']
                brand_cust = grocery.loc[j, 'BRAND']
                brand4.append(tech.loc[i, 'Brand'])
                #message.append('Brand matched, UoM, Unit of Weight didnt match')
                match = 4 

            elif match != 1 and match != 2 and match != 3 and match != 4 and similarity[0][0]>=0.75 \
            and brand_similarity[0][0]<0.75 \
            and uom_ratio >= 0.75 and unit_weight_cust == unit_weight_smer: 

                print('I am in Match 5')
                print (tech.loc[i, 'Product lower']+"------"+grocery.loc[j, 'Item lower'])
                print (str(tech.loc[i, 'Brand'])+"------"+str(grocery.loc[j, 'BRAND']))
                price = tech.loc[i, 'SKT- Unit Price']
                print(price)

                prices5.append(price)
                item = grocery.loc[j, 'ITEM NAME']
                brand_cust = grocery.loc[j, 'BRAND']
                brand5.append(tech.loc[i, 'Brand'])
                #message.append('Brand didnt match, Unit Weight, UoM matched')
                match = 5  

            elif match != 1 and match != 2 and match != 3 and match != 4 and match != 5 \
            and similarity[0][0]>=0.75 and \
            brand_similarity[0][0]<0.75 \
            and uom_ratio >= 0.75 and unit_weight_cust != unit_weight_smer: 

                print('I am in Match 6')
                print (tech.loc[i, 'Product lower']+"------"+grocery.loc[j, 'Item lower'])
                print (str(tech.loc[i, 'Brand'])+"------"+str(grocery.loc[j, 'BRAND']))
                price = tech.loc[i, 'SKT- Unit Price']
                print(price)

                prices6.append(price)
                item = grocery.loc[j, 'ITEM NAME']
                brand_cust = grocery.loc[j, 'BRAND']
                brand6.append(tech.loc[i, 'Brand'])
                #message.append('UoM matched, Brand, Unit Weight didnt match')
                match = 6  

            elif match != 1 and match != 2 and match != 3 and match != 4 and match != 5 and match != 6 \
            and similarity[0][0]>=0.75 \
            and brand_similarity[0][0]<0.75 \
            and uom_ratio < 0.75 and unit_weight_cust == unit_weight_smer: 

                print('I am in Match 7')
                print (tech.loc[i, 'Product lower']+"------"+grocery.loc[j, 'Item lower'])
                print (str(tech.loc[i, 'Brand'])+"------"+str(grocery.loc[j, 'BRAND']))
                price = tech.loc[i, 'SKT- Unit Price']
                print(price)

                prices7.append(price)
                item = grocery.loc[j, 'ITEM NAME']
                brand_cust = grocery.loc[j, 'BRAND']
                brand7.append(tech.loc[i, 'Brand'])
                #message.append('UoM, Brand didnt match, Unit Weight matched')
                match = 7 

            elif match != 1 and match != 2 and match != 3 and match != 4 and match != 5 and match != 6 \
            and match != 7 and match != 100 and match != 101 and match != 102 and match != 103 \
            and similarity[0][0]>=0.75 and \
            brand_similarity[0][0]<0.75 \
            and uom_ratio < 0.75 and unit_weight_cust != unit_weight_smer: 

                print('I am in Match 8')
                print (tech.loc[i, 'Product lower']+"------"+grocery.loc[j, 'Item lower'])
                print (str(tech.loc[i, 'Brand'])+"------"+str(grocery.loc[j, 'BRAND']))
                price = tech.loc[i, 'SKT- Unit Price']
                print(price)

                prices8.append(price)
                item = grocery.loc[j, 'ITEM NAME']
                brand_cust = grocery.loc[j, 'BRAND']
                brand8.append(tech.loc[i, 'Brand'])
                #message.append('Brand, UoM, Unit Weight didnt match')
                match = 8        

        except:
            #print('')
            pass


    if match ==100: 
        max_val = max(prices100)
        index_max = prices100.index(max_val)
        price_final.append(max_val) 
        #grocery.loc[j, 'Inter Price'] = max(prices)
        item_name.append(item)
        #brands_smer='None'
        brands_cust.append(brand_cust)
        brand_smer = brand100[index_max]
        print(brand_smer)
        brands_smer.append(brand_smer)
        messages.append('No brand specified by customer, UoM and Unit Weight matched')
        count = count+1
        match = 0
        print('#'*80)

    elif match ==101: 
        max_val = max(prices101)
        index_max = prices101.index(max_val)
        price_final.append(max_val) 
        #grocery.loc[j, 'Inter Price'] = max(prices)
        item_name.append(item)
        #brands_smer='None'
        brands_cust.append(brand_cust)
        brand_smer = brand101[index_max]
        print(brand_smer)
        brands_smer.append(brand_smer)
        messages.append('No brand specified by customer, UoM matched, Unit Weight didnt match')
        count = count+1
        match = 0
        print('#'*80) 

    elif match ==102: 
        max_val = max(prices102)
        index_max = prices102.index(max_val)
        price_final.append(max_val) 
        #grocery.loc[j, 'Inter Price'] = max(prices)
        item_name.append(item)
        #brands_smer='None'
        brands_cust.append(brand_cust)
        brand_smer = brand102[index_max]
        print(brand_smer)
        brands_smer.append(brand_smer)
        messages.append('No brand specified by customer, UoM didnt match, Unit Weight matched')
        count = count+1
        match = 0
        print('#'*80)  

    elif match ==103: 
        max_val = max(prices103)
        index_max = prices103.index(max_val)
        price_final.append(max_val) 
        #grocery.loc[j, 'Inter Price'] = max(prices)
        item_name.append(item)
        #brands_smer='None'
        brands_cust.append(brand_cust)
        brand_smer = brand103[index_max]
        print(brand_smer)
        brands_smer.append(brand_smer)
        messages.append('No brand specified by customer, UoM, Unit Weight didnt match')
        count = count+1
        match = 0
        print('#'*80)     

    elif match==1:
        price_final.append(max(price1)) 
        #grocery.loc[j, 'Inter Price'] = max(prices)
        item_name.append(item)
        brands_smer.append(brand_smer)
        brands_cust.append(brand_cust)
        messages.append('Brand, UoM, Unit Weight matched')
        count = count+1
        match = 0
        print('#'*80)

    elif match ==2: 
        max_val = max(prices2)
        index_max = prices2.index(max_val)
        price_final.append(max_val) 
        #grocery.loc[j, 'Inter Price'] = max(prices)
        item_name.append(item)
        #brands_smer='None'
        brands_cust.append(brand_cust)
        brand_smer = brand2[index_max]
        print(brand_smer)
        brands_smer.append(brand_smer)
        messages.append('Brand, UoM matched, Unit Weight didnt match')
        count = count+1
        match = 0
        print('#'*80)

    elif match ==3: 
        max_val = max(prices3)
        index_max = prices3.index(max_val)
        price_final.append(max_val) 
        #grocery.loc[j, 'Inter Price'] = max(prices)
        item_name.append(item)
        #brands_smer='None'
        brands_cust.append(brand_cust)
        brand_smer = brand3[index_max]
        print(brand_smer)
        brands_smer.append(brand_smer)
        messages.append('Brand, Unit Weight matched, UoM didnt match')
        count = count+1
        match = 0
        print('#'*80) 

    elif match ==4: 
        max_val = max(prices4)
        index_max = prices4.index(max_val)
        price_final.append(max_val) 
        #grocery.loc[j, 'Inter Price'] = max(prices)
        item_name.append(item)
        #brands_smer='None'
        brands_cust.append(brand_cust)
        brand_smer = brand4[index_max]
        print(brand_smer)
        brands_smer.append(brand_smer)
        messages.append('Brand matched, Unit Weight, UoM didnt match')
        count = count+1
        match = 0
        print('#'*80) 

    elif match ==5: 
        max_val = max(prices5)
        index_max = prices5.index(max_val)
        price_final.append(max_val) 
        #grocery.loc[j, 'Inter Price'] = max(prices)
        item_name.append(item)
        #brands_smer='None'
        brands_cust.append(brand_cust)
        brand_smer = brand5[index_max]
        print(brand_smer)
        brands_smer.append(brand_smer)
        messages.append('Brand didnt match, Unit Weight, UoM matched')
        count = count+1
        match = 0
        print('#'*80)  

    elif match ==6: 
        max_val = max(prices6)
        index_max = prices6.index(max_val)
        price_final.append(max_val) 
        #grocery.loc[j, 'Inter Price'] = max(prices)
        item_name.append(item)
        #brands_smer='None'
        brands_cust.append(brand_cust)
        brand_smer = brand6[index_max]
        print(brand_smer)
        brands_smer.append(brand_smer)
        messages.append('Brand, Unit weight didnt match, UoM matched')
        count = count+1
        match = 0
        print('#'*80) 

    elif match ==7: 
        max_val = max(prices7)
        index_max = prices7.index(max_val)
        price_final.append(max_val) 
        #grocery.loc[j, 'Inter Price'] = max(prices)
        item_name.append(item)
        #brands_smer='None'
        brands_cust.append(brand_cust)
        brand_smer = brand7[index_max]
        print(brand_smer)
        brands_smer.append(brand_smer)
        messages.append('Brand, UOM didnt match, Unit Weight matched')
        count = count+1
        match = 0
        print('#'*80) 

    elif match ==8: 
        max_val = max(prices8)
        index_max = prices8.index(max_val)
        price_final.append(max_val) 
        #grocery.loc[j, 'Inter Price'] = max(prices)
        item_name.append(item)
        #brands_smer='None'
        brands_cust.append(brand_cust)
        brand_smer = brand8[index_max]
        print(brand_smer)
        brands_smer.append(brand_smer)
        messages.append('Brand, UOM, Unit Weight didnt match')
        count = count+1
        match = 0
        print('#'*80)    



print(count) 
print('-'*80)

data = {'Item': item_name,'Purchase Price': price_final, 'Brand_Smerkato': brands_smer, 'Brand_Customer': brands_cust, 'Message': messages}
df_final = pd.DataFrame(data,columns=['Item','Purchase Price','Brand_Smerkato', 'Brand_Customer', 'Message'])
print(df_final)
print('-'*80)

df_final.to_csv('Item_list_match.csv', encoding='utf-8', index=False)

The output I am getting for this code is (copied partially as it's very long):

uom_ratio 0.000000
name similarity 0.000000
brand similarity 0.000000
BASMATI RICE XXL
Dry Fruits & Edible Seeds
AASHIRVAD
Dates
unit_weight_cust 1
uom_cust KGS
uom_smer 1
unit_weight_smer nan
--------------------------------------------------------------------------------
uom_ratio 0.000000
name similarity 0.000000
brand similarity 0.000000
BASMATI RICE XXL
Dry Fruits & Edible Seeds
AASHIRVAD
Dates
unit_weight_cust 1
uom_cust KGS
uom_smer 1
unit_weight_smer nan
--------------------------------------------------------------------------------
0
--------------------------------------------------------------------------------
Empty DataFrame
Columns: [Item, Purchase Price, Brand_Smerkato, Brand_Customer, Message]
Index: []
--------------------------------------------------------------------------------

Aucun commentaire:

Enregistrer un commentaire