Reputation: 19
I'm having a headache by trying to access data in a dictionary. I'm not sure what I call dictionary is really a correct one but at least the following structure doesn't give me error.
From a file configuration.py
...
appList = {
"app1": {
"appCode": "xyz",
"appName": "name1",
"appType": "ChromeWebApp",
"appAddress": '/opt/google/chrome/google-chrome \"--profile-directory=Profile {0}\"'.format(v.profil),
"appID": " --app-id=abcdefgh123456",
"appRemoteDebug": ' --remote-debugging-port=9222',
"functions": {
"action1",
"action2",
"action3"}
},
"app14": {
"appCode": "xyz",
"appName": "name1",
"appType": "python",
"appAddress": '/bin/python3 /home/folder/app.py',
"appID": " --app-id=qwertyui48965",
"appRemoteDebug": ' --remote-debugging-port=9222',
"functions": {
"action17",
"action29",
"action39"}
}
}
def checkUser(dico, user):
if user in dico:
return True
return False
def checkArgs(dico, webapp, functions, function):
if webapp in dico:
if functions in dico[webapp]:
if function in dico[webapp][functions]:
return True
return False
a script main.py is requiring 3 arguments
parser = argparse.ArgumentParser(...)
group = parser.add_mutually_exclusive_group()
parser.add_argument("-u", type=str, dest='u', help="the user")
parser.add_argument("-a", type=str, dest='a', help="the app")
parser.add_argument("-f", type=str, dest='f', help="the function")
args = parser.parse_args()
arguments are then checked as per the dictionaries located in the first file (config.py) and data which are matching the arguments are accessed and will feed a third file variables.py
def varDefining():
v.user = args.u
v.appCode = args.a
v.appFunc = args.f
v.profil = [c.userList][v.user]["profil"]
v.appID = [c.appList][v.appCode]["appID"]
v.appPath = [c.appList][v.appCode]["appAddress"]
v.appRemoteDebug = [c.appList][v.appCode]["appRemoteDebug"]
if args.quiet:
if c.checkUser(c.userList, args.u) == True:
if c.checkArgs(c.appList, args.a, "functions", args.f) == True:
varDefining()
During a couple of thousand of trying and error i used to get the following error message:
TypeError: list indices must be integers or slices, not NoneType
I can get it for example with this: print([c.appList][args.a]) Which most likely means the way i'm accesing the data is not correct or maybe the dictionaries themselves are not properly formated...
I'm actually trying to properly code my Python scripts that's why there are three files: main, config and variables which is maybe a wrong way, I haven't see this in a tutorial that's come from my own imagination. So my first need is obviously getting rid of the list indices error but any comments on my style of coding and recommendations or even a link to doc where it's talking about the subject are welcome.
Thanks
Upvotes: 1
Views: 125
Reputation: 51643
You create a list and index into it by v.user
and ["profile"]
- you can only index into lists with integers or slices:
def varDefining(): v.user = args.u v.appCode = args.a v.appFunc = args.f v.profil = [c.userList][v.user]["profil"] # creates & indexes in list # etc
This puts c.userList
into a list and immediately tries to index into it by the value of v.user
( == args.u
) - which (in your case) is None
. Even if not, it would most likely be a string due to
parser.add_argument("-u", type=str, dest='u', help="the user")
The error has nothing to do with your dictionary.
You essentially do something like:
a = [1,2,3,4]["2"] # can not work, index not an integer or slice
which produces your exact error message:
SyntaxWarning: list indices must be integers or slices, not str; perhaps you missed a comma? a = [1,2,3,4]["2"]
Upvotes: 1