I have a function with many input parameters, and I need a function that will return a list of parameter names (not values) for each parameter whose value is ” or None
Normally I’d throw an exception in such a method. If anyone wants to crack the problem by throwing an exception, that is fine. I still have the requirement that the function return the list of parameter names.
To summarize
- Return a list of parameter names for parameters that are unset
- “unset” means the parameter’s value is not empty string or None
- Accept a single parameter: a single dimension list or dict
- The list should contain the complete set of empty parameter names
- I need it to be backward compatible with Python 2.2 and Jython
- 2.2 is non-negotiable. The code must run on legacy systems that we have no authority to upgrade. Sucks to be us.
- The parameters are not command line arguments, but parameters to a
function. - The parameters are stored in individual variables, but I can manually put them into a dict if necessary.
- Instead of returning a list of Python variable names, return a list of user-friendly descriptions for each empty variable. Example: “Database Name” vs “db_name”.
Answers to questions raised:
- What if an unknown parameter is encountered? We don’t care. We create the list of parameters to validate and select only those which are mandatory by virtue of the system’s logic. Thus we’d never put an unknown parameter into the list of ones to validate
- What about UI parameters that are not mandatory or which must be validated in other ways (int vs. string, etc)? We would not put the non-mandatory params in the list we pass to the validation function. For other more complex validations, we handle these individually, adhoc. The reason this function seemed convenient is because empty parameters are the most common validation we do, and writing an
if not foo:for each one gets tedious across functions, of which we have many. -
Please explain “””By nature of our platform”””. Also “””it arrives in individual variables””” … individual variables in what namespace? And what does “””(preprocessing)””” mean? – John Machin 2 days ago. Answer: The variables are in the global namespace. We use code injection (similar to how a C preprocessor would substitute code for macro names, except we are substituting variable values for tags, similar to this:
DATABASE_NAME = ^-^Put the variable the user entered for database name here^-^
which ends up like this after the preprocessor runs:
DATABASE_NAME = "DB1"
Here is a concrete example showing why a simple method throwing an exception would not work. I have rewritten to use an exception rather than returning a value, by request:
def validate_parameters(params_map):
"""
map is like {foo: "this is foo"}
"""
missing_params_info = []
for k,v in params_map.items():
if not k:
missing_params_info.append(v)
if missing_params_info:
raise TypeError('These parameters were unset: %s' % missing_params_info)
params = {}
params['foo'] = '1'
params['bar'] = '2'
params['empty'] = ''
params['empty2'] = ''
params['None'] = None
params_map = {
params['foo']: 'this is foo',
params['bar']: 'this is bar',
params['empty']: 'this is empty',
params['empty2']: 'this is empty2',
params['None']: 'this is None',
}
print validate_parameters(params_map)
bash-3.00# python /var/tmp/ck.py
Traceback (most recent call last):
File "/var/tmp/ck.py", line 26, in ?
print validate_parameters(params_map)
File "/var/tmp/ck.py", line 10, in validate_parameters
raise TypeError('These parameters were unset: %s' % missing_params_info)
TypeError: These parameters were unset: ['this is empty2', 'this is None']
Two reasons it doesn’t work for us: It only prints empty2, even though there is another empty parameter, “empty”. “empty” is overwritten by “empty2” because they use the same key in the map.
Second reason: I need to get the list of descriptions into a variable at some point after running this function. Maybe this is possible with exceptions, but I don’t know how right now.
I’ve posted an answer that seems to solve all these problems, but is not ideal. I marked the question answered, but will change that if someone posts a better answer.
Thanks!
I’m pretty sure I don’t understand the question or how what you posted as your ‘best solution’ meets the requirements, but working just from:
Here’s an easy way to do what that line seems to ask for:
and then just call validate_parameters from the first line of a function:
If it wasn’t for the Python 2.2 requirement you could do it all in a single line list comprehension. The important thing is that you have to call it from the very first line of the function to ensure that
locals()only picks up parameters and not any other local variables.