I want to write a generic function that calls other functions and does some additional pre and post processing.
Example calls:
genericfn(function=max,a=3,b=4) would call max(a=3,b=4) and return 4
genericfn(function=average,a=4,c=8) would call average(a=4,c=8) and return 6, assuming parameter b in fn average defaults to 6
It should handle any user-defined function with a variable number of keyword args.
Here's the code I have so far, but I don't know how to get the dictionary to convert to a series of keyword args.
def genericfn(*args,**kwargs):
fnToApply = kwargs['function']
del kwargs['function']
return fnToApply(args,kwargs)
Thanks for your help.
2 1700 bvdet 2,851
Expert Mod 2GB
I want to write a generic function that calls other functions and does some additional pre and post processing.
Example calls:
genericfn(function=max,a=3,b=4) would call max(a=3,b=4) and return 4
genericfn(function=average,a=4,c=8) would call average(a=4,c=8) and return 6, assuming parameter b in fn average defaults to 6
It should handle any user-defined function with a variable number of keyword args.
Here's the code I have so far, but I don't know how to get the dictionary to convert to a series of keyword args.
def genericfn(*args,**kwargs):
fnToApply = kwargs['function']
del kwargs['function']
return fnToApply(args,kwargs)
Thanks for your help.
Something like this: - import operator
-
-
def genericfn(*args,**kwargs):
-
fnToApply = kwargs['function']
-
del kwargs['function']
-
return fnToApply(*args)
-
-
dd = {'function': max}
-
print genericfn(1,2,3,4,5,**dd)
-
-
dd = {'function': operator.mul}
-
print genericfn(7,9,**dd)
>>> 5
63
>>>
-
def someFunction(*args, **kwargs):
-
"someFunction"
-
print 'args: ',args
-
print 'kwargs: ',kwargs
-
-
def factoryFunction(func, *args, **kwargs):
-
print 'Starting %s'%func.__doc__ # code before
-
values = func(*args, **kwargs)
-
print 'Finishing %s'%func.__doc__ # code after
-
return values
-
-
>>> factoryFunction(someFunction, 1,2,3,['a','list'], greeting='hello', number=1)
-
Starting someFunction
-
args: (1, 2, 3, ['a', 'list'])
-
kwargs: {'greeting': 'hello', 'number': 1}
-
Finishing someFunction
-
-
>>> args = (someFunction, 1,2,3,['a','list'])
-
>>> kwargs = {'greeting':'hello', 'number':1}
-
>>> factoryFunction(*args, **kwargs)
-
Starting someFunction
-
args: (1, 2, 3, ['a', 'list'])
-
kwargs: {'greeting': 'hello', 'number': 1}
-
Finishing someFunction
-
' *' creates a tuple of all extra the values and ' *' can also turn a tuple back into arguments
' **' creates a dictionary of all the extra keywords and ' **' can also turn a dictionary back into keywords
I would recommend requiring the function be passed in and save yourself the trouble of having to extract and remove it.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Gabriel Zachmann |
last post by:
Is there any generic way to use C++ libraries from within Python.
I seem to recall that there are tools to generate wrappers for C-libraries
semi-automatically.
But those were still way too...
|
by: Andreas Huber |
last post by:
What follows is a discussion of my experience with .NET generics & the
..NET framework (as implemented in the Visual Studio 2005 Beta 1),
which leads to questions as to why certain things are the...
|
by: .Net Newbie |
last post by:
I'm new to .Net and need to create a generic (free) way to update lookup
tables in SQL Server (using C#) in ASP.Net pages. I found an article at:...
|
by: Urs Eichmann |
last post by:
While experimenting with the Feb CTP Edition of VB 2005, I came across
"generic procedures". You can write:
Public Class Foo
Public Sub MySub(Of tDisp As IDisposable)(ByVal vMyParm As Integer)...
|
by: Anders Borum |
last post by:
Hello!
Whilst refactoring an application, I was looking at optimizing a
ModelFactory with generics. Unfortunately, the business objects created by
the ModelFactory doesn't provide public...
|
by: Greg Buchholz |
last post by:
/*
I've been experimenting with some generic/polytypic programs, and
I've stumbled on to a problem that I can't quite figure out. In the
program below, I'm trying to define a generic version of...
|
by: tshad |
last post by:
This is a little complicated to explain but I have some web services on a
machine that work great.
The problem is that I have run into a situation where I need to set up my
program to access one...
|
by: Charles Churchill |
last post by:
I apologize if this question has been asked before, but after about
half an hour of searching I haven't been able to find an answer online.
My code is beloiw, with comments pertaining to my...
|
by: Noah Roberts |
last post by:
I need to create a log file that logs calls to a certain API. The
obvious way to do this is to put a bunch of output functions
before/after calls but it seems to me that there should be a generic...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |