class CustomException(Exception): pass def validate_params(params, req_keys=[]): """ Verify if all required keys are present in the dictionary passed to this method :param params: Parameter Dictionary to check :param req_keys: These Keys are required in the dictionary :rtype: Boolean :returns: True or False """ if all(_ in params for _ in req_keys): return True raise CustomException("Not all keys present in dictionary") try: good = {'reqKey': 'yeahh', 'somethingelse': 'something'} result = validate_params(good, req_keys=['reqKey']) print(result) except CustomException: print("Exception thrown") try: bad = {'key1': 'value1'} result = validate_params(bad, req_keys=['reqKey']) print(result) except CustomException: print("Exception thrown")
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question