Home > Software engineering >  How to write this code without repetition?
How to write this code without repetition?

Time:09-13

What would be an easier and cleaner way to write this code below in Python? I know there has to be a simpler way to write multiple Or statements in Python but can't figure out one that suits my example.

def liquid_add_to_cart(liquid):
    actions.sleep_for_secs(1.5)
    
    if liquid.__contains__("Water"):
        add_water_btn_x, add_water_btn_y = pyautogui.locateCenterOnScreen('pictures/shopping/add_to_cart_btn.png',
                                                                          confidence=.8)
        pyautogui.click(add_water_btn_x, add_water_btn_y)
        pyautogui.click(button='left', clicks=water_cap, interval=2.0)
    
     elif liquid.__contains__("Juice"):
        add_juice_btn_x, add_juice_btn_y = pyautogui.locateCenterOnScreen('pictures/shopping/add_to_cart_btn.png',
                                                                          confidence=.8)
        pyautogui.click(add_juice_btn_x, add_juice_btn_y)
        pyautogui.click(button='left', clicks=juice_cap, interval=2.0)
    

      elif liquid.__contains__("Milk"):
        add_milk_btn_x, add_milk_btn_y = pyautogui.locateCenterOnScreen('pictures/shopping/add_to_cart_btn.png',
                                                                        confidence=.8)
        pyautogui.click(add_milk_btn_x, add_milk_btn_y)
        pyautogui.click(button='left', clicks=milk_cap, interval=2.0)

CodePudding user response:

I am assuming that the centre coordinates you are using in the if elif statements go out of scope after the execution of the statements.

If not, just add them as function parameters in the code below:

path = 'pictures/shopping/add_to_cart_btn.png'

def liq(liq_cap):
    add_liq_btn_x, add_liq_btn_y = pyautogui.locateCenterOnScreen(path, confidence=.8)
    pyautogui.click(add_liq_btn_x, add_liq_btn_y)
    pyautogui.click(button='left', clicks=liq_cap, interval=2.0)

def liquid_add_to_cart(liquid):
    actions.sleep_for_secs(1.5)
    
    if liquid.__contains__("Water"):
        liq(water_cap)
    
    elif liquid.__contains__("Juice"):
        liq(juice_cap)
    
    elif liquid.__contains__("Milk"):
        liq(milk_cap)

CodePudding user response:

Something like this perhaps?

liqdict = {
    "Water": [add_water_btn_x, add_water_btn_y, water_cap],
    "Juice": [add_juice_btn_x, add_juice_btn_y, juice_cap],
    "Milk": [add_milk_btn_x, add_milk_btn_y, milk_cap]
}

def liquid_add_to_cart(liquid, liqdict):
    actions.sleep_for_secs(1.5)

    for key in liqdict.keys:
        if liquid.__contains__(key):
            btn_x = liqdict[key][0]
            btn_y = liqdict[key][1]
            cap = liqdict[key][2]
            btn_x, btn_y = pyautogui.locateCenterOnScreen(
                'pictures/shopping/add_to_cart_btn.png',
                confidence=.8)
            pyautogui.click(btn_x, btn_y)
            pyautogui.click(button='left', clicks=cap, interval=2.0)
                 break

You have't revealed how these globals are defined, so this might not work right out of the box; but hopefully this should get you moving in the right direction. Perhaps more generally you should arrange your data structures into a dictionary rather than a long list of global variables.

CodePudding user response:

You need a control structure. In this case a list of tuples should suffice.

CONTROL = [('Water', water_cap), ('Juice', juice_cap), ('Milk', milk_cap)]
PNG = 'pictures/shopping/add_to_cart_btn.png'

def liquid_add_to_cart(liquid):
    actions.sleep_for_secs(1.5)

    for k, v in CONTROL:
        if k in liquid:
            btn_x, btn_y = pyautogui.locateCenterOnScreen(PNG, confidence=0.8)
            pyautogui.click(btn_x, btn_y)
            pyautogui.click(button='left', clicks=v, interval=2.0)
            # break here if the keyword can only exist once in liquid

Assumption that water_cap, juice_cap and milk_cap (whatever they are?) are in scope.

What this means is that the function code doesn't need to change. All you need to do is add appropriate values to the CONTROL list

  • Related