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