I am trying to write a Python script that accepts a string list of criteria, compares it to values in a dictionary, and executes if the criteria are met (or it throws an error message).
An example criteria list and dictionary values are given below:
criteria_list = ['A < 5', 'B == 6', '3 < C < 4']
dict = {'A': 3, 'B': 6, 'C': 3.5, 'D': 5, 'E': 100}
My initial thought was to have load in the values of the dictionary that are relevant to the criteria by checking if the criteria values are in the dictionary. I could then check if the value satisfies the criteria and then execute the code. If the criteria value is not in the dictionary then throw an error (i.e. criteria_list = [..., "Z == 10", ...] > throws an error that Z is not in the dictionary).
This is what I have now, but I'm having trouble finding a way to save the key value locally to use the eval(criteria) to check if the criteria is True or False.
crit_sum = 0
for criteria in criteria_list:
for key in dict.keys():
if (key in criteria.split()):
#SAVE KEY VALUE (i.e. A = 3)
if eval(criteria):
crit_sum += 1
elif (key not in critera.split()):
print("criteria not in dictionary!")
if crit_sum == len(criteria_list): #execute code if all criteria are TRUE
#EXECUTE CODE
Let me know what you think a good way of saving values locally or another way that could work for evaluating an arbitrary sized criteria list.