lundi 22 mars 2021

How to use defined String in for loop in Python

I have a question regarding for loop and string in Python.

I have defined 2 vars for configuration during json format -

new_repository_config_0 = {
    "name": "bla777",
    "online": "true",
    "storage": {
        "blobStoreName": "nexus-blobstore",
        "strictContentTypeValidation": "true",
        "writePolicy": "allow_once"
    },
    "maven": {
        "versionPolicy": "SNAPSHOT",
        "layoutPolicy": "STRICT"
    }
}

new_repository_config_1 = {
    "name": "bla888",
    "online": "true",
    "storage": {
        "blobStoreName": "nexus-blobstore",
        "strictContentTypeValidation": "true",
        "writePolicy": "allow_once"
    },
    "maven": {
        "versionPolicy": "SNAPSHOT",
        "layoutPolicy": "STRICT"
    }
}

Also I have defined a function, and I want to use those conf vars in main.

I want to do it this way -

repositories = get_resource(config=config, api_type='repositories')
for i in range(1):
    string = 'config.new_repository_config_%d' % (i,)
    print (string)
    if string['name'] not in \
            [repository['name'] for repository in repositories]:
        create_repo(
           config=config, repository=string)

Basically, the if statement line, once must be -

if config.new_repository_config_0['name']

And the second time, it must be -

if config.new_repository_config_1['name']

But since, I'm making conversion to String, I'm getting this error -

"if string['name'] not in \ TypeError: string indices must be integers"

Is there a way to solve it in Python?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire