Home > Back-end >  get item from dict and count number of items in the list
get item from dict and count number of items in the list

Time:07-12

I used jsondiff.diff() to compare the flattened items (as a json) from two jsons of the mapping of an Elasticsearch index. One is what I loaded, the original mapping I created, and the other is the mapping queried from Elasticsearch, so it could technically be different if the data inserted was different from expected. The mappings are identical, so to test finding the diff I added 2 different fields to the original mapping.
Using jsondiff.diff() works beautifully, and I find my differences.

diff(dfpulledlist, dfloadedlist)
diff(dfloadedlist, dfpulledlist)

That produces:

{insert: [(114, 'mappings.properties.saw_something.properties.monster_fish'), (121, 'mappings.properties.aliens')]}

It looks like a dictionary. Type() tells me it is a <class 'dict'>, but trying to get the items under insert gives me errors. I therefore printed the items and found something strange. Now I'm trying to figure out how to get to the items of insert so I can count them and don't know how to work my way through this.

print(diff_pulled_to_loaded.items())

Gives me:

dict_items([(insert, [(114, 'mappings.properties.saw_something.properties.monster_fish'), (121, 'mappings.properties.aliens')])])

Looking at this page, it looks like it is a dictionary from sequence having each item as a pair. I can't access it as a dictionary though. dict['insert'] or dict.insert . On each attempt I get KeyError: 'insert'.

How do I get to the values of insert, which is an array or list of tuples, so I can query that information? What am I missing/misunderstanding here? I want to get to this:

[(114, 'mappings.properties.saw_something.properties.monster_fish'),
 (121, 'mappings.properties.aliens')]

CodePudding user response:

It's a dictionary, but keys like insert and replace are not strings, they're instances of the jsondiff.Symbol class. That's why they don't have quotes around them -- this class has a custom representation that just returns the symbol name.

So to access it I think you have to use

d = diff(dfpulledlist, dfloadedlist)
print(d[jsondiff.insert])
  • Related