Building arguments for function calls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dberg
    New Member
    • Jun 2007
    • 2

    Building arguments for function calls

    Hi all-

    I have a function call that uses keyword arguments:

    lib_routine( status=computed _value, hw_id=value[1], sw_id=value[2], \
    something=value[3], something_else= value[4]...... )

    There can be as many as 10 different keywords with the =value[n] construct. There can also be as few as 2 keywords passed to the function (status and one of the =value[n] constructs). Of course, anything between the 2 and 10 values are valid. Parsing a string of user-supplied text fills in the "value" list.

    How do I construct an argument list for the function call that only contains keywords that contain data (i.e., if value[3] == "", I do not want to have the something=value[3] in the argument list).

    Any hints/pointers would be greatly appreciated.

    Thanks,
    Dave
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by dberg
    Hi all-

    I have a function call that uses keyword arguments:

    lib_routine( status=computed _value, hw_id=value[1], sw_id=value[2], \
    something=value[3], something_else= value[4]...... )

    There can be as many as 10 different keywords with the =value[n] construct. There can also be as few as 2 keywords passed to the function (status and one of the =value[n] constructs). Of course, anything between the 2 and 10 values are valid. Parsing a string of user-supplied text fills in the "value" list.

    How do I construct an argument list for the function call that only contains keywords that contain data (i.e., if value[3] == "", I do not want to have the something=value[3] in the argument list).

    Any hints/pointers would be greatly appreciated.

    Thanks,
    Dave
    This sounds like a job for a ..............D ICTIONARY!
    Code:
    >>> dd = {'value1': 12, 'value2': '', 'value3': 'a string', 'value4': ['a', 1, '5', 12.438]}
    >>> dd1 = {}
    >>> for key in dd:
    ... 	if dd[key] != '':
    ... 		dd1[key] = dd[key]
    ... 		
    >>> def keyword_args(**kargs):
    ... 	for key in kargs:
    ... 		print key, kargs[key]
    ... 		
    >>> keyword_args(**dd1)
    value4 ['a', 1, '5', 12.438000000000001]
    value3 a string
    value1 12
    >>>

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by dberg
      Hi all-

      I have a function call that uses keyword arguments:

      lib_routine( status=computed _value, hw_id=value[1], sw_id=value[2], \
      something=value[3], something_else= value[4]...... )

      There can be as many as 10 different keywords with the =value[n] construct. There can also be as few as 2 keywords passed to the function (status and one of the =value[n] constructs). Of course, anything between the 2 and 10 values are valid. Parsing a string of user-supplied text fills in the "value" list.

      How do I construct an argument list for the function call that only contains keywords that contain data (i.e., if value[3] == "", I do not want to have the something=value[3] in the argument list).

      Any hints/pointers would be greatly appreciated.

      Thanks,
      Dave
      Use ** to "de-reference" any dict. My friend bvdet may be along shortly to show cool usage of zip to create dictionaries, but in the meantime:
      >>> def funcWithKeyWord Args(**kwargs):
      ... print kwargs
      ...
      >>> funcWithKeyWord Args(**{"keywor d":5})
      {'keyword': 5}
      >>>

      Comment

      • dberg
        New Member
        • Jun 2007
        • 2

        #4
        Hi bvdet and bartonc-

        Thank you very much for the help. Yes, the dictionary (and ** ) was exactly what I was looking for - and it worked perfectly.

        Much thanks,
        Dave

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by dberg
          Hi bvdet and bartonc-

          Thank you very much for the help. Yes, the dictionary (and ** ) was exactly what I was looking for - and it worked perfectly.

          Much thanks,
          Dave
          You are very welcome. Drop on in any ol' time.

          Comment

          Working...