I looked at many of the exisiting nameErrors and undefined variable posts and unfortunately was unable to resolve my issue. I am relatively new to programming and especially Python.
I have a dictionary of dictionaries defined in informcast_data.py:
allLinks = {'Send or Edit Messages': {'link':'/InformaCast/admin?cmd=listMessages', 'page_title': 'Messages: Send or Edit Messages'}
I am importing informacast_data.py to users.py:
from informacast.informacast_data import ICData
I then have a function, verify_links which has the following for statement:
for href_link in hrefs:
if href_link.find(ICData.allLinks['page_title']['link'])!=-1:
self.logger.debug("verify_links found=True for " + str(href_link))
found=True
If I leave quotes around page_title I get a keyError and I was told to remove the quotes as it is a variable not a string, I get the nameError when I remove the quotes and I am not sure how to proceed.
below is the full traceback:
Traceback (most recent call last):
File "C:\Users\jesse.sole\Jesse_Workspace\trunk\src\informacast\tests\users.py", line 17, in test_appadmin_role
self.verify_role("appAdmin")
File "C:\Users\jesse.sole\Jesse_Workspace\trunk\src\informacast\tests\users.py", line 78, in verify_role
self.verify_links(sel, roleName)
File "C:\Users\jesse.sole\Jesse_Workspace\trunk\src\informacast\tests\users.py", line 104, in verify_links
if href_link.find(ICData.allLinks[page_title]['link'])!=-1:
NameError: global name 'page_title' is not defined
Thank you in advance for any assistance.
allLinksis a nested dictionary. The top level has nopage_titlekey, so that’s why you get a key error:There is such a key in the nested dictionary though:
I think you want to define a variable
page_titlethat has the value'Send or Edit Messages':If you loop over the keys in
allLinksthat would work: