Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6896093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:59:28+00:00 2026-05-27T06:59:28+00:00

I have a function with many input parameters, and I need a function that

  • 0

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

  1. Return a list of parameter names for parameters that are unset
  2. “unset” means the parameter’s value is not empty string or None
  3. Accept a single parameter: a single dimension list or dict
  4. The list should contain the complete set of empty parameter names
  5. I need it to be backward compatible with Python 2.2 and Jython
  6. 2.2 is non-negotiable. The code must run on legacy systems that we have no authority to upgrade. Sucks to be us.
  7. The parameters are not command line arguments, but parameters to a
    function.
  8. The parameters are stored in individual variables, but I can manually put them into a dict if necessary.
  9. 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:

  1. 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
  2. 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.
  3. 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!

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:59:29+00:00Added an answer on May 27, 2026 at 6:59 am

    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:

    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

    Here’s an easy way to do what that line seems to ask for:

    def validate_parameters(args):
        unset = []
        for k in args:
            if args[k] is None or args[k]=="":
                unset.append(k)
        return unset
    

    and then just call validate_parameters from the first line of a function:

    def foo(a, b, c):
        print "Unset:", validate_parameters(locals())
    
    >>> foo(1, None, 3)
    Unset: ['b']
    >>> foo(1, None, "")
    Unset: ['c', 'b']
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a function that has a parameter that is overloaded by many
I have an old function that is called many times in my application. I
Many people have argued about function size. They say that functions in general should
I am a web developer, and I have observed that many times I need
I have the following script $(function(){ $('.input1_class, .input2_class, <many other input classes go here>').bind('change',
i have many events such as $('.hidefile').click(function (event) { On page load and on
I have many structs (classes) and standalone functions that I like to compile separately
Not many are aware of this feature, but Python's functions (and methods) can have
i have function public Menu Details(int? id) { return _dataContext.Menu.Include(ChildMenu).FirstOrDefault(m => m.MenuId == id);
Ok so i have a form that has three input boxes, at the moment

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.