title: How can I write a generic Python 2.2 function that returns a list of unset parameters?
body: <p>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</p>
  
  <p>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.</p>
  
  <p>To summarize</p>
  
  <ol>
  <li>Return a list of parameter names for parameters that are unset</li>
  <li>"unset" means the parameter's value is not empty string or None</li>
  <li>Accept a single parameter: a single dimension list or dict</li>
  <li>The list should contain the complete set of empty parameter names</li>
  <li>I need it to be backward compatible with Python 2.2 and Jython</li>
  <li>2.2 is non-negotiable.  The code must run on legacy systems that we have no authority to upgrade.  Sucks to be us.</li>
  <li>The parameters are not command line arguments, but parameters to a
  function.</li>
  <li>The parameters are stored in individual variables, but I can manually put them into a dict if necessary.</li>
  <li>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".</li>
  </ol>
  
  <p>Answers to questions raised:</p>
  
  <ol>
  <li>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</li>
  <li>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 <code>if not foo:</code> for each one gets tedious across functions, of which we have many.</li>
  <li><p>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:</p>
  
  <p>DATABASE_NAME = ^-^Put the variable the user entered for database name here^-^</p></li>
  </ol>
  
  <p>which ends up like this after the preprocessor runs:</p>
  
  <pre><code>DATABASE_NAME = "DB1"
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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']
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Thanks!</p>