Python Forum
How do I access values from a dict without it being in a massive for loop?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I access values from a dict without it being in a massive for loop?
#1
I'm trying to write a basic testing framework in python using a spreadsheet for data and selenium webdriver to do my tests.

I've managed to get so far with reading in my spreadsheet into a list of dictionaries and putting it into my own function called cell_value, which I'm quite pleased about. The second argument of this function is an integer to call each row. The only way I can see to call each row, run some selenium script and then call the next row is to put it in a loop. The problem I have is all my selenium code has to be inside this loop. As do all my variables for storing the data e.g. web_address. I can't put the data variables outside the loop (ideally in a global variables block at the start of the script) as I'd have to specify a specific row for the function to work. Locking me in to one row all the time.

Is there any way of just having a loop present to give me an integer, run some code, then give me the next one -so I can then put it in my cell_value function wherever I want? I'd like to keep the selenium code outside the loop in a class and the data variables (e.g. web_address) at the start of the script.

Also could someone tell me why if statement (cell_value("Run", i) == 'N' or 'n') always returns Y? It's driving me mad.

I hope this makes sense, as I've tried to explain best I can.

Many thanks.

Below is my code:

    from xlrd import open_workbook
    from selenium import webdriver
    
    #** Global Variables **#
    book = open_workbook('test_sheet_YN.xls')
    sheet = book.sheet_by_index(0)
    
    
    def cell_value(name, run_row):
        first_row = []  # The row where we store the name of the column
        for col in range(sheet.ncols):
            first_row.append(sheet.cell_value(0, col))
        data = []
        for row in range(run_row):  # number is brackets is row number, starts at 1 (not zero) - it's start, stop, step.
            dict = {}
            for col in range(sheet.ncols):
                dict[first_row[col]] = sheet.cell_value(row, col)
            data.append(dict)
        return dict[name]  # 'name' is the sheet column-header that returns the value of the specified row, 'run_row'
    
    for i in range(2,sheet.nrows+1):
        print cell_value("URL", i) + " #row# " + str(i)
        web_address = cell_value("URL", i)
        name_of_URL = cell_value("Name", i)
        if cell_value("Run", i) == 'Y' or 'y':
            print "jump to selenium class"
            # for now just do this...
             driver = webdriver.Chrome('/usr/local/chromedriver-Linux64')
             driver.get(web_address)
             driver.implicitly_wait(10)
             driver.quit()
        elif cell_value("Run", i) == 'N' or 'n':
            print "Don't run"
        elif cell_value("Run", i) != 'N' or 'n' or 'Y' or 'y':
            print "enter something useful: Y or N"
    
        else:
            pass
Reply
#2
fruit = {
    'Bananas': {
        'color': 'yellow',
        'serving': 'one'
    },
    'Grapes': {
        'color': 'red',
        'serving': 'ten'
    }
}

print('Bananas are {}'.format(fruit['Bananas']['color']))

# in pyton 3.6
# print(f"Bananas are {fruit['Bananas']['color']}")
Reply
#3
Thanks, I'll have a play. That's really helpful :-D
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020