473,395 Members | 1,442 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,395 software developers and data experts.

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 1534
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="first", 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
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...
4
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...
31
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...
1
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...
3
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...
14
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() '...
2
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...
1
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....
1
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...
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: 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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...

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.