Add to nested dictionary within loop? (Python)

moosemaimer

Ars Scholae Palatinae
820
So I'm not a programmer, I took a few classes ages ago, but there are some Python scripts in use on the servers here so I figured I'd start.

What I'm trying to do is call an API and get a chunk of JSON data, then store it in a nested dictionary, as I iterate through a list of strings. (000, 001, 004, etc.) The API call and data storage is working fine (it's how I got the list to begin with), but I can't figure out how to add each API response to inhabit a separate "folder" in the dictionary.

VumUaue.png


So this is where I'm at, having banged my head against it for a day. The first loop works fine and creates a whole series of headers in the dictionary (so I know that works), but as soon as I start the second loop it removes them all, and as it adds each chunk of data from the API call it removes the previous one, so that by the end only the final header and its data are present. Is there any way to make this work, or would I be better off using separate dicts for each iteration?
 

moosemaimer

Ars Scholae Palatinae
820
What is activeagents?
A list consisting of 3-character strings, starting with 000 and ending with 037. The two print() statements at the end were to test if it is actually populating the dictionary properly, and it isn't, which is why 000 throws an "out of range" error.


I don't know much python, but it sure looks like line 48 is replacing the entire vulns array (?) with the response for the most recent activeagent each time through the loop. Maybe it should be vulns[k] = {k: ...}?

You are right there. Though it should be vulns[k] = json....
well damn, that works, if I remove the curly braces which I thought needed to be there. I had tried that before and got a different error.
 
I had tried that before and got a different error.

You didn't try that before, or it would have worked. You tried something similar to that, but goofed up the syntax somehow. We've all done it.

I'm pointing it out because it's a common failure; to throw something at a solution without fully understanding, making a minor mistake and then abandoning it and running after the wrong solution instead.

I've had jr coworkers do this, and then argue with me when I point out their mistake, instead they try to force me or others into using their solution, even though it's the wrong methodology.

Not a shot at you personally, you came here looking for help and took it graciously.

You should take the knowledge you have now, and you should go back to the reference materials you have for Python and review the syntax for what you are accomplishing here. You might get a better understanding of the syntax, or, you might have a better understanding of how to read the reference material.

Back in the day when C compilers were expensive and came with printed books outlining all the functions I spent an inordinate amount of time looking through the function lists, and getting a better understanding of how to interpret the methodologies used.
 

moosemaimer

Ars Scholae Palatinae
820
I meant "I had tried (that syntax, but with the curly braces) before," as well as a number of other things I found on multiple tutorials and wikis. Most of the information I was finding about how to get data into a dictionary was starting with a pre-existing list, whereas the way this tool is intended to work I have no guarantee any value will be present. Was getting a whole host of errors: unhashable, expected [int] not [str], and was starting to think what I was attempting wasn't going to work. But it is now, and I'm getting useful data out of the dictionary, like dumping everything in the 'name' field into a list and converting it to a set, so I have unique values I can count occurrences of.
 
I think it's also worth pointing out that your first loop is no longer needed. That loop is creating a load of empty dictionaries, then your second loop is overwriting those dictionaries with new ones that have your desired data already contained within.

If counting is the task at hand then the Counter class might be of use too: https://docs.python.org/3/library/collections.html#collections.Counter
 

moosemaimer

Ars Scholae Palatinae
820
First loop is now gone, it was the result of an earlier attempt that was giving me an error. Came across the Counter class while investigating, will likely work that in somewhere so I don't have to use multiple nested loops. Once I get all the logic worked out I'll start moving things into functions, and other files, as that should save on memory by doing most of the heavy lifting with local variables.