So I have a method that takes a dictionary object as a parameter. I want to check over the dictionary to make sure it has all the correct keys and if not throw a custom exception. The code I have so far works but I am thinking there might be a better way. (Using Python 2.7)
def my_method(temp_dict):
#this is the part that looks messy and I would like to improve
if "key" not in temp_dict:
raise CustomException("Message key is missing from dict")
if "key1" not in temp_dict:
raise CustomException("Message key1 is missing from dict")
if "key2" not in temp_dict:
raise CustomException("Message key2 is missing from dict")
if "key3" not in temp_dict:
raise CustomException("Message key3 is missing from dict")
my_dict = {}
my_dict["key"] = "test"
my_dict["key1"] = "test"
my_dict["key2"] = "test"
my_method(my_dict)
Link to Python Fiddle Link