Last active
August 29, 2015 14:23
-
-
Save nelsnelson/0e3478c107b309e57638 to your computer and use it in GitHub Desktop.
What is the best way to verify that a specific set of named keyword argument parameters of a method are not None, and be able to refer to those parameters, by name, in a log message reporting the missing parameters, but not causing the method to explicitly fail?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _possibly_deprecated_someday( | |
required_a, | |
required_b, | |
named_only_for_readability_a=None, | |
named_only_for_readability_b=None, | |
named_only_for_readability_c=None, | |
named_only_for_readability_d=None): | |
"""Does some complicated stuff.""" | |
required_keywords = [ | |
'named_only_for_readability_a', | |
'named_only_for_readability_b', | |
'named_only_for_readability_c', | |
'named_only_for_readability_d' | |
] | |
missing_parameters = [x for x in required_keywords if eval(x) is None] | |
if len(missing_parameters) > 0: | |
log.warning('Missing %s parameter(s) for ' | |
'base.module#_possibly_deprecated_someday(...). ' | |
'Returning -1.0.' % ", ".join(missing_parameters)) | |
return -1.0 # Which means "N/A" | |
complex_stuff = [] | |
return complex_stuff |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def log_missing(method, failure_case, *parameters): | |
log.warning('Missing %s parameter(s) for %s.' % (", ".join(parameters), method)) | |
return failure_case() | |
@ensure_keyword_arguments_are_present(failure_case=lambda { | |
return -1.0 # Which means "N/A". | |
}) | |
def _possibly_deprecated_someday( | |
required_a, | |
required_b, | |
named_only_for_readability_a=None, | |
named_only_for_readability_b=None, | |
named_only_for_readability_c=None, | |
named_only_for_readability_d=None): | |
"""Does some complicated stuff.""" | |
complex_stuff = [] | |
return complex_stuff |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment