This pushed me for a while to Python, there is no standard way of output if the provided parameter is None or has an absent value, and also does not process the JSON / Dict object gracefully,
for example, I want to display the actual parameter name in the error message,
username = None if not username: log.error("some parameter is missing value")
It is not possible to pass the actual parameter name unless you do this artificially and randomly by hard-coding the parameter in the error message, i.e.
if not username: log.error("username is missing value")
but it is random and prone to syntax errors, and the pain in the butt to maintain.
For this reason, I wrote the Dictator function,
https://medium.com/@mike.reider/python-dictionaries-get-nested-value-the-sane-way-4052ab99356b
If you add your parameters to the dict or read your parameters from the YAML or JSON configuration file, you can tell the Dictator to raise the ValueError value if the parameter is null,
eg,
config.yaml
skills: sports: - hockey - baseball
now your py program reads in this configuration file, and parameters, like JSON dict,
with open(conf_file, 'r') as f: config = yaml.load(f)
now set your parameters and also check if theyre NULL
sports = dictator(config, "skills.sports", checknone=True)
If the sport is not None, it will raise the ValueError value, telling you which parameter is missing
ValueError("missing value for ['skills']['sports']")
you can also provide a fallback value for your parameter, so if it is None, give it a fallback default value,
sports = dictator(config, "skills.sports", default="No sports found")
This avoids the ugly exceptions for Index / Value / Key errors.
Its flexible, elegant way of processing large dictionary data structures, and also gives you the ability to check your program parameters for Null values ββand display the actual parameter names in an error message