473,729 Members | 2,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling function keywords from a dictionary

Hello!

I'm fooling around with a class wich can map cgi-parameters to
function named parameter.

I have a dic like
dic = {
'action': 'myfunc',
'x': 1,
'y': 2,
'z': 3
}
and I have a method
def myfunc(x, y, z, a=None, b=None)

What I want to do ist calling the method defined in 'action' with
all the named parameter, also instead of
self.myfunct(x= 1, y=2, z=3)
I want to do
f = getattr(self, dic['action'])
f(named parameter from the dic)
Its a little like zope does it, but i didnt find it in the Zope Sources ;-)
How can do that?

Thanks, AXEL.
Jul 18 '05 #1
2 1546
Alexander Straschil wrote:
I have a dic like
dic = {
'action': 'myfunc',
'x': 1,
'y': 2,
'z': 3
}
and I have a method
def myfunc(x, y, z, a=None, b=None)
I assume you forgot the self parameter.
What I want to do ist calling the method defined in 'action' with
all the named parameter, also instead of
self.myfunct(x= 1, y=2, z=3)
I want to do
f = getattr(self, dic['action'])
f(named parameter from the dic)

class A: .... def first(self, x, y, z, a=None, b=None):
.... print "calling first"
.... print "x =", x
.... print "a =", a
.... a = A()
d = dict(action="fi rst", x=1, y=2, z=3)
d {'y': 2, 'x': 1, 'z': 3, 'action': 'first'}
# as a sideeffect, pop() removes "action" from the dictionary
# make a copy if that bothers you bm = getattr(a, d.pop("action") )
bm(**d) calling first
x = 1
a = None d["a"] = 99
bm(**d)

calling first
x = 1
a = 99

Peter
Jul 18 '05 #2
Thanks, that was exaclty what I needed, works cool now!

AXEL.
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2160
by: Andrew Wilkinson | last post by:
Hi, First off I know that in almost all cases this would be a terrible thing to do, but this is an unusual case where this makes sense. Basically I have a procedure where you pass a string containting a template for a tuple, the function then finds a tuple that matches this template and then returns it. The template may contain variable names, which match against anything in that position in the tuple - this is all working fine,...
4
1399
by: Doug Holton | last post by:
First let me say please see the wiki page about python decorators if you haven't already: http://www.python.org/cgi-bin/moinmoin/PythonDecorators I propose (and others have) that built-in features have keyword support, like static and class methods. Also, I believe it is more readable if decorators, especially longer ones, are moved to the top of the function body, just like docstrings, instead of coming before the function is even...
31
2432
by: Brian Sabbey | last post by:
Here is a pre-PEP for what I call "suite-based keyword arguments". The mechanism described here is intended to act as a complement to thunks. Please let me know what you think. Suite-Based Keyword Arguments ----------------------------- Passing complicated arguments to functions is currently awkward in Python. For example, the typical way to define a class property winds up polluting the class's namespace with the property's get/set...
1
1576
by: Stephen Kellett | last post by:
Hello everyone, I'm trying to do something in C calling Python and its failing. I'd be grateful if you could take a look and hopefully you have an answer. What I'm trying to do is determine the address of the "collect" function in the "gc" module. I want to do this so that we can determine when a garbage collection happens, regardless of how it is triggered (explicitly by a user call, or implicitly by the behaviour of the program...
3
4540
by: Varun | last post by:
Hi There, I have a form("myRequest.asp") and the values from it are retrieved into the page ("output_Print.asp") on which I have two buttons('Save As Complete' and 'Save As Incomplete'). When the 'Save as Incomplete' button is Clicked the form will be going to the "SaveAsincomplete.asp" without validation of the fields. And when the 'save as complete' is clicked certain fileds are to be validated and by the function return value, if false...
14
3007
by: ericellsworth | last post by:
Hi, I'm trying to use a class to pass variables back and forth from a form opened in dialog mode. I have created a class which invokes a form in its show method, like so: Public Sub Show() ' This method shows the form used to get the info If sWhereInt = "" Then DoCmd.OpenForm sFormNameInt, acNormal, , , acFormAdd, _
2
1670
by: azriley | last post by:
could use some help with a function to clean up keywords in a url. I'm expecting $keywords to equal 'coretta scott king' - the plus signs are stripped out for search engine referrals. What am I doing wrong? <?php $referer = 'http://www.google.com/search?hl=en&q=coretta+scott+king'; function parse_keywords($referer){
1
1475
by: Andy Wu | last post by:
Hi All, I've been working on a domain parking project where we need to analyze a domain name, say "bookhotel", and get keywords(book, hotel) out of it, then use these keywords to do some search. There are two ways to do this as far as I can see, longest match and quickest match. Longest match: bookhotel -check with a dictionary -False
1
2212
by: joeedh | last post by:
Hi I'm getting extremely odd behavior. First of all, why isn't PyEval_EvalCode documented anywhere? Anyway, I'm working on blender's python integration (it embeds python, as opposed to python embedding it). I have a function that executes a string buffer of python code, fetches a function from its global dictionary then calls it. When the function code returns a local variable, PyObject_Call() appears to be returning garbage. ...
0
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9281
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.