473,372 Members | 1,070 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 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 1092
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

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

Similar topics

8
by: Birgit Rahm | last post by:
Hallo Newsgroup, I have the following problem: I work with Python 2.2 and invoke functions via CORBA ( I use onmiORB/omniORBpy) on a server. The server provides me a function, where the 3...
3
by: David Hughes | last post by:
I'm nearly there building a Python extension module to access a set of functions in a .dll, but I'm getting an unresolved external error for one of the function calls (using MS Visual C++ V6). ...
9
by: Matt Eberts | last post by:
Sorry, bad title. Anyway, is there a way to pass the arguments to an object instantiated via a constructor using the arguments object and have it expanded, so to speak, so that it doesn't appear as...
6
by: Gianni Mariani | last post by:
Before I go deep into this - does anyone have the quick fix for this ? Some facts - the 7.3.4 version of plperl.c has the same errors in the 7.4 tree. The 7.4 version of plperl.c (with some...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
11
by: Neo | last post by:
Why the following code is compilable? The function abc() doesn't take any arguments, still i can call the function with arbitraty number of arguments. Even compiler doesn't show any warning. What...
9
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script...
36
by: Pacific Fox | last post by:
Hi all, haven't posted to this group before, but got an issue I can't work out... and hoping to get some help here ;-) I've got a base object that works fine with named arguments when called...
2
by: Spoon | last post by:
Hello everyone, AFAIU, in C89, calling fprintf with too few arguments leads to UB. 4.9.6.1 The fprintf function Synopsis #include <stdio.h> int fprintf(FILE *stream, const char *format,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.