471,348 Members | 1,250 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Building arguments for function calls

2
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
Jun 14 '07 #1
4 1022
bvdet
2,851 Expert Mod 2GB
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 ..............DICTIONARY!
Expand|Select|Wrap|Line Numbers
  1. >>> dd = {'value1': 12, 'value2': '', 'value3': 'a string', 'value4': ['a', 1, '5', 12.438]}
  2. >>> dd1 = {}
  3. >>> for key in dd:
  4. ...     if dd[key] != '':
  5. ...         dd1[key] = dd[key]
  6. ...         
  7. >>> def keyword_args(**kargs):
  8. ...     for key in kargs:
  9. ...         print key, kargs[key]
  10. ...         
  11. >>> keyword_args(**dd1)
  12. value4 ['a', 1, '5', 12.438000000000001]
  13. value3 a string
  14. value1 12
  15. >>> 
Jun 14 '07 #2
bartonc
6,596 Expert 4TB
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 funcWithKeyWordArgs(**kwargs):
... print kwargs
...
>>> funcWithKeyWordArgs(**{"keyword":5})
{'keyword': 5}
>>>
Jun 14 '07 #3
dberg
2
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
Jun 14 '07 #4
bartonc
6,596 Expert 4TB
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.
Jun 14 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Birgit Rahm | last post: by
3 posts views Thread by David Hughes | last post: by
6 posts views Thread by Gianni Mariani | last post: by
9 posts views Thread by Mikhail Teterin | last post: by
9 posts views Thread by Csaba Gabor | last post: by
36 posts views Thread by Pacific Fox | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.