1

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MatMat
  • 19
  • 2
  • In which line of code the error is occurred? And also include the whole code in one block. – Nouman Ahsan Nov 10 '21 at 06:16
  • I'm sure that the error is in the `varDefining()` method because you are not accessing elements in the dictionary instead you are accessing elements in the `c.userList` which is a list probably. Correct me if I'm wrong. – Nouman Ahsan Nov 10 '21 at 06:24
  • unrelated: `def checkUser(dico, user): return user in dico` – Patrick Artner Nov 10 '21 at 06:26
  • `args.a` from the parser will either be the default `None`, or a user supplied string. – hpaulj Nov 10 '21 at 06:38

1 Answers1

1

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"]

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Thanks Patrick, actually and i guess you understood, in def vardefining() i store the 3 arguments u, a and f intoa file: variables.py in user, appCode and appFunc. – MatMat Nov 10 '21 at 11:32
  • Thanks Patrick, actually and i guess you understood, in def vardefining() i store the 3 arguments u, a and f into a file: variables.py in the variables: user, appCode and appFunc. With these 3 arguments i need profil, appiD, appPath and appRemoteDebug by simply getting the values from the 2 dictionaries in the file configuration.py. The following v.profil = c.userList[v.user].get("profil") returns me KeyError: None which means that the value of the key profil doesn't exist and it is (not shown on my extract but it is) and if it returns the same error with the others – MatMat Nov 10 '21 at 11:57