I understand basically what the message means, but I don’t understand how it applies in this context.
At this point, I would appreciate any pointers, especially if you have tackled this error message before.
As an introduction, my app “reads” a culinary recipe as a text block. It parses the nutritional elements, like ingredients, and returns a nutritional profile. The process is accurate, but it is not perfect.
For this reason, I send those elements (ingredients, units of measurements, amounts, modifiers) to an editable datatable after initial processing for the user to edit and/or approve the data before the final analysis.
For the last couple of days, I’ve been trying to enhance this datatable by using Python fuzzywuzzy (now thefuzz?) to add additional options to the ingredients column with dropdown menus inside those cells. In other words - instead of one ingredient - there will be a dropdown for each ingredient plus its top four matches in the database.
The code was working until it came time to populate the ordered dictionaries.
Does the message mean the lists for the dropdown menus must be the same length?
That makes no sense to me. There are two dropdown lists in the datatable now.: ingredients (five options) and modifiers (three options). The lists that populate the ingredients row are generated by fuzzy logic, the one for the modifiers are static, as shown below. The rest have only one option.
Does it mean missing data causes some iterations of the dictionary to have fewer fields than others?
But I am baffled. The data in these ordered dictionaries goes through a rigorous validation process to ensure that each field has some value (even ‘None’) before it gets to this stage in the process. But missing values are still a possible cause for the error message. I don’t know what else it could mean.
I have run out of insights. Can anyone shed any light on its meaning?
I have pasted a minimal, meaningful example of the code below. It is the “template” for the ordered dictionary, populated via a for loop with single values for some row items and lists for the drop downs. Based on the video, that should not produce this error, though. Dropdowns with different list sizes are permissible.
Here is the code:
# For each list in the list of lists of dictionaries, create an ordered dictionary
necessary to populate the datatable. In this case, the ing_lists are six ingredients
related to each other at last according to thefuzz. That part may need some work,
but each ing_lists is precisely six ingredients. All the same size.
for ing_lists in ing_matches:
tobe_edited_datatable_ordict = pd.DataFrame(OrderedDict([
('Index', ingdex),
('Amount', amount),
('Unit', units),
('Modifier', ['small', 'medium', 'large']),
# This is the only generated list.
('Ingredient', ing_lists),
('Ing_id', ing_id)
]))
# 7. Place ordered dicts inside list.
tobe_edited_list.append(tobe_edited_datatable_ordict)
# 8. Eliminate any dupes from tobe_edited_list
unique_recipe_data_list = list(unique_everseen(tobe_edited_list))
# 9. Save list in JSON file.
with open("./json/tobe_edited_ordered_ing_dicts.json", "w") as ordered_recipe_data:
json.dump(unique_recipe_data_list, ordered_recipe_data, indent=4)
# 10. Print out list of ordered dicts.
with open("./json/tobe_edited_ordered_ing_dicts.json", "r") as ordered_recipe_data:
ord_dd_dicts = json.load(ordered_recipe_data)
Here is an example of an ing_lists - this one for red lentils:
MATCH LISTS
[('red lentils', 100), ('lentils', 90), (' california red kidney beans', 86), (' cans of hot red chili peppers', 86), (' cans of red kidney beans added fat', 86)]
Here is a snippet of the entire Traceback message: