Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Directly calling python's function arguments dispatcher

Reply
Thread Tools

Directly calling python's function arguments dispatcher

 
 
Pascal Chambon
Guest
Posts: n/a
 
      12-12-2010
Hello

I've encountered several times, when dealing with adaptation of function
signatures, the need for explicitly resolving complex argument sets into
a simple variable mapping. Explanations.


Consider that function:

def foo(a1, a2, *args, **kwargs):
pass

calling foo(1, a2=2, a3=3)

will map these arguments to local variables like these:
{
'a1': 1,
'a2': 2,
'args': tuple(),
'kwarg's: {'a3': 3}
}

That's a quite complex resolution mechanism, which must handle
positional and keyword arguments, and deal with both collision and
missing argument cases.

Normally, the simplest way to invoke this mechanism is to define a
function with the proper signature, and then call it (like, here, foo()).

But there are cases where a more "meta" approach would suit me well.

For example when adapting xmlrpc methods : due to the limitations of
xmlrpc (no keyword arguments), we use a trick, i.e our xmlrpc functions
only accept a single argument, a "struct" (python dict) which gets
unpacked on arrival, when calling the real functions exposed by the
xmlrpc server.

But on client side, I'd like to offer a more native interface (allowing
both positional and keyword arguments), without having to manually
define an adapter function for each xmlrpc method.

To summarize, I'd like to implement a magic method like this one (please
don't care about performance isues for now):

class XmlrpcAdapter:
def __getattr__(self, funcname):
# we create an on-the-fly adapter
def adapter(*args, **kwargs):
xmlrpc_kwargs = _resolve_func_signature(funcname, *args,
**kwargs)
# we call the remote function with an unique dict argument
self.xmlrpc_server.call(funcname, xmlrpc_kwargs)
return adapter

As you see, all I need is _resolve_func_signature(), which is actually
the routine (internal to the python runtime) which transforms complex
function calls in a simple mapping of variables to be added to the
function local namespace. Of course this routine would need information
about the target functions' signature, but I have that info available
(for example, via a set of functions that are a mockup of the real
xmlrpc API).

Is that routine exposed to python, somewhere ? Does anybody know a
working implementation here or there ?

Thanks for the help,
regards,
Pakal



 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      12-12-2010
Pascal Chambon wrote:


> I've encountered several times, when dealing with adaptation of function
> signatures, the need for explicitly resolving complex argument sets into
> a simple variable mapping. Explanations.
>
>
> Consider that function:
>
> def foo(a1, a2, *args, **kwargs):
> pass
>
> calling foo(1, a2=2, a3=3)
>
> will map these arguments to local variables like these:
> {
> 'a1': 1,
> 'a2': 2,
> 'args': tuple(),
> 'kwarg's: {'a3': 3}
> }
>
> That's a quite complex resolution mechanism, which must handle
> positional and keyword arguments, and deal with both collision and
> missing argument cases.


> Is that routine exposed to python, somewhere ? Does anybody know a
> working implementation here or there ?


http://docs.python.org/library/inspe...ct.getcallargs

 
Reply With Quote
 
 
 
 
Pascal Chambon
Guest
Posts: n/a
 
      12-13-2010
Le 12/12/2010 23:41, Peter Otten a écrit :
> Pascal Chambon wrote:
>
>
>
>> I've encountered several times, when dealing with adaptation of function
>> signatures, the need for explicitly resolving complex argument sets into
>> a simple variable mapping. Explanations.
>>
>>
>> Consider that function:
>>
>> def foo(a1, a2, *args, **kwargs):
>> pass
>>
>> calling foo(1, a2=2, a3=3)
>>
>> will map these arguments to local variables like these:
>> {
>> 'a1': 1,
>> 'a2': 2,
>> 'args': tuple(),
>> 'kwarg's: {'a3': 3}
>> }
>>
>> That's a quite complex resolution mechanism, which must handle
>> positional and keyword arguments, and deal with both collision and
>> missing argument cases.
>>

>
>> Is that routine exposed to python, somewhere ? Does anybody know a
>> working implementation here or there ?
>>

> http://docs.python.org/library/inspe...ct.getcallargs
>
>

Too sweeeeeeeeeet \o/

Thanks a lot,
regards,
Pakal
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a function that expects variable arguments from a functionwith variable arguments Navaneeth C Programming 4 11-20-2010 05:35 AM
how to pass a function name and its arguments inside the arguments of other function? jmborr Python 1 11-03-2007 08:20 AM
Calling VBScript function directly from ASP.NET murali ASP .Net 1 11-21-2006 04:28 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Preview image directly on PC, save directly to HD Patrick M. Digital Photography 3 01-07-2004 08:29 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57