Home > Mobile >  Python - For Loop not iterating through multiple instances of a dictionary
Python - For Loop not iterating through multiple instances of a dictionary

Time:12-30

I have the following code

def generate_matter(leaver_user,vaultAccessToken):
    for user in leaver_user:
        url = "https://vault.googleapis.com/v1/matters/"

        headers = {
        "Accept" : "application/json",
        "Content-Type" : "application/json",
        "Authorization": "Bearer "   vaultAccessToken
        }

        body = json.dumps ({           
        "state": "OPEN",
        "description": "Generated by Python",
        "name": user   "'s archive"
        })

        response = requests.request(
        "POST",
        url,
        headers=headers,
        data=body
        )

        jsonContent = json.loads(response.text)
        matterID=jsonContent["matterId"]
        #print("Matter ID for "   user   " is "   matterID)
        #print(jsonContent)

        matter={
            "matterInstance": {
                "user": user,
                "userInfo": {
                    "matterID": matterID
            }
            
            }
        }

    return matter

def generate_search_query(matter,leaver_user,vaultAccessToken):
    print(matter)
    for key, value in matter.items():
        user=(matter['matterInstance']['user'])
        matterID=(matter['matterInstance']['userInfo']['matterID'])
        url = "https://vault.googleapis.com/v1/matters/" matterID "/savedQueries"
        
        headers = {
        "Accept" : "application/json",
        "Content-Type" : "application/json",
        "Authorization": "Bearer "   vaultAccessToken
        }

        body=json.dumps({
            "displayName": user   "'s email search query",
            "query": {
                "corpus": "MAIL",
                "dataScope": "ALL_DATA",
                "searchMethod": "ACCOUNT",
                "accountInfo": { "emails": [user]},
                "mailOptions": {"excludeDrafts" : "false"},
                "timeZone": "Atlantic/Canary",
                "method": "ACCOUNT"
        }}
        )

        response = requests.request(
        "POST",
        url,
        headers=headers,
        data=body
        )
        jsonContent = json.loads(response.text)
        print(matterID)
        print(body)
        print(jsonContent)
        savedQueryID=jsonContent["savedQueryId"]
        print("savedQueryId for "   user   " is "   savedQueryID   " matterID is "   matterID)
        

        matter={
            "matterInstance": {
                "user": user,
                "userInfo": {
                    "matterID": matterID,
                    "savedQueryID": savedQueryID
            }
            
            }
        }

    return matter
matter=generate_matter(leaver_user,vaultAccessToken)
savedQueryID=generate_search_query(matter,leaver_user,vaultAccessToken)

What works is the first function - generate_matter().

This returns multiple instances of matter such as

{'matterInstance': {'user': '[email protected]', 'userInfo': {'matterID': '12-34-56-78-91'}}}
{'matterInstance': {'user': '[email protected]', 'userInfo': {'matterID': '12-34-56-78-99'}}}

However the function generate_search_query() only seems to execute on the first instance of matter.

I've confirmed this by printing the matter in generate_search_query() before the for loop executes and only the first instance of matter is returned.

 {'matterInstance': {'user': '[email protected]', 'userInfo': {'matterID': '12-34-56-78-91'}}}

Adding from the below comments as its useful information.

Printing matter within the for loop from generate_matter does return multiple instances of matter.

Printing matter immediately before calling savedQueryID=generate_search_query(matter,leaver_user,vaultAccessToken) only returns a single instance of matter, so this is when i print / call it outside of the function

How would I solve this so that multiple instances of matter are executed in the for loop within generate_search_query() ?

Thanks

CodePudding user response:

At the end of generate_matter() you're overwriting matter with the last iteration, and then returning it, so its only returning a single element.

To fix this, create a list at the start of generate_matter() (matterList = []) and then where you have matter={...} in generate_matter() replace it with matterList.append({...}). Then at the end of the function return matterList instead of matter.

In generate_search_query() you'll need to wrap everything in another for loop to loop through the list.

  • Related