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

call to function by text variable

yeah the subject doesn't really make sense does it?

anyway want I want to do is this:
if n == 1:

self.operations.insert(pos, operations.Replace.Panel(self, main))

elif n == 2:

self.operations.insert(pos, operations.ChangeCase.Panel(self,
main))

elif n == 3:

self.operations.insert(pos, operations.Move.Panel(self, main))

As you can see all the different functions have the same variables, so
it would be easier if I could just make a list and use that.

like this:
list = ["Replace", "ChangeCase", "Move"]
textVariable = list[n]
self.operations.insert(pos, operations.[textVariable].Panel(self,
main))

Is something sort of like that possible?
TIA

Mar 25 '07 #1
12 1315
ianaré wrote:
like this:
list = ["Replace", "ChangeCase", "Move"]
textVariable = list[n]
self.operations.insert(pos, operations.[textVariable].Panel(self,
main))

Is something sort of like that possible?
Yes:

self.operations.insert(
pos,
getattr(operations, textVariable).Panel(self.main)
)
Mar 25 '07 #2
ianaré wrote:
yeah the subject doesn't really make sense does it?

anyway want I want to do is this:
if n == 1:

self.operations.insert(pos, operations.Replace.Panel(self, main))

elif n == 2:

self.operations.insert(pos, operations.ChangeCase.Panel(self,
main))

elif n == 3:

self.operations.insert(pos, operations.Move.Panel(self, main))

As you can see all the different functions have the same variables, so
it would be easier if I could just make a list and use that.

like this:
list = ["Replace", "ChangeCase", "Move"]
textVariable = list[n]
self.operations.insert(pos, operations.[textVariable].Panel(self,
main))

Is something sort of like that possible?
Indeed. You don't need to use textual names (though for that you can
investigate "getattr()) - the following, naturally, is untested:

ops = [operations.Replace,
operations.ChangeCase,
operations.Move
]
self.operations.insert(pos, ops[n-1].Panel(self, main)

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com
Mar 25 '07 #3
ianaré wrote:
yeah the subject doesn't really make sense does it?

anyway want I want to do is this:
if n == 1:

self.operations.insert(pos, operations.Replace.Panel(self, main))

elif n == 2:

self.operations.insert(pos, operations.ChangeCase.Panel(self,
main))

elif n == 3:

self.operations.insert(pos, operations.Move.Panel(self, main))

As you can see all the different functions have the same variables, so
it would be easier if I could just make a list and use that.

like this:
list = ["Replace", "ChangeCase", "Move"]
textVariable = list[n]
self.operations.insert(pos, operations.[textVariable].Panel(self,
main))

Is something sort of like that possible?
Indeed. You don't need to use textual names (though for that you can
investigate "getattr()) - the following, naturally, is untested:

ops = [operations.Replace,
operations.ChangeCase,
operations.Move
]
self.operations.insert(pos, ops[n-1].Panel(self, main)

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Mar 25 '07 #4
En Sun, 25 Mar 2007 19:36:26 -0300, ianaré <ia****@gmail.comescribió:
list = ["Replace", "ChangeCase", "Move"]
textVariable = list[n]
self.operations.insert(pos, operations.[textVariable].Panel(self,
main))

Is something sort of like that possible?
Try getattr:
textVariable = "Replace"
getattr(operations, textVariable) == operations.Replace

Perhaps you could just store the result in your list; instead of
["Replace", "ChangeCase", "Move"], use [operations.Replace,
operations.ChangeCase, operations.Move] (it's not always applicable, of
course: maybe operations is reassigned, or those attributes mutate or
don't always exist...)

--
Gabriel Genellina

Mar 25 '07 #5
On Mar 25, 3:36 pm, "ianaré" <ian...@gmail.comwrote:
yeah the subject doesn't really make sense does it?

anyway want I want to do is this:
if n == 1:

self.operations.insert(pos, operations.Replace.Panel(self, main))

elif n == 2:

self.operations.insert(pos, operations.ChangeCase.Panel(self,
main))

elif n == 3:

self.operations.insert(pos, operations.Move.Panel(self, main))

As you can see all the different functions have the same variables, so
it would be easier if I could just make a list and use that.

# Your list would contain the unbound functions:

unbound_funcs = [operations.Replace.Panel,
operations.Change.Panel,
operations.Move.Panel]
# and invocation would be:

self.operations.insert(pos, unbound_funcs[n - 1](self, main))
--
Hope this helps,
Steven
Mar 25 '07 #6
Hi,

try this:
func = getattr(operations, ["Replace", "ChangeCase", "Move"][n])

HTH,
Jan

"ianaré" <ia****@gmail.comschreef in bericht
news:11**********************@p15g2000hsd.googlegr oups.com...
yeah the subject doesn't really make sense does it?

anyway want I want to do is this:
if n == 1:

self.operations.insert(pos, operations.Replace.Panel(self, main))

elif n == 2:

self.operations.insert(pos, operations.ChangeCase.Panel(self,
main))

elif n == 3:

self.operations.insert(pos, operations.Move.Panel(self, main))

As you can see all the different functions have the same variables, so
it would be easier if I could just make a list and use that.

like this:
list = ["Replace", "ChangeCase", "Move"]
textVariable = list[n]
self.operations.insert(pos, operations.[textVariable].Panel(self,
main))

Is something sort of like that possible?
TIA

--
http://mail.python.org/mailman/listinfo/python-list


Mar 25 '07 #7
Cool now I can run it through the translator.

ops = (_("Directory"), _("Replace"), _("ChangeCase"),
_("Move"), _("Swap"), _("Insert"), _("ChangeLength"))
self.operations.insert(pos, getattr(operations, ops[n]).Panel(self,
main))

Thanks guys!

Mar 25 '07 #8
On Mar 25, 7:01 pm, "ianaré" <ian...@gmail.comwrote:
Cool now I can run it through the translator.

ops = (_("Directory"), _("Replace"), _("ChangeCase"),
_("Move"), _("Swap"), _("Insert"), _("ChangeLength"))

self.operations.insert(pos, getattr(operations, ops[n]).Panel(self,
main))

Thanks guys!
erm ... brainfart LOL. But now I can link it to the translated list.

Mar 25 '07 #9
In article <ma***************************************@python. org>,
Jan Schilleman <ja************@xs4all.nlwrote:
>Hi,

try this:
func = getattr(operations, ["Replace", "ChangeCase", "Move"][n])

HTH,
Jan

"ianaré" <ia****@gmail.comschreef in bericht
news:11**********************@p15g2000hsd.googleg roups.com...
>yeah the subject doesn't really make sense does it?

anyway want I want to do is this:
if n == 1:

self.operations.insert(pos, operations.Replace.Panel(self, main))
Mar 26 '07 #10
Cameron Laird wrote:
In article <ma***************************************@python. org>,
Jan Schilleman <ja************@xs4all.nlwrote:
>Hi,

try this:
func = getattr(operations, ["Replace", "ChangeCase", "Move"][n])

HTH,
Jan

"ianaré" <ia****@gmail.comschreef in bericht
news:11**********************@p15g2000hsd.googleg roups.com...
>>yeah the subject doesn't really make sense does it?

anyway want I want to do is this:
if n == 1:

self.operations.insert(pos, operations.Replace.Panel(self, main))
.
.
.
I think you meant "...[n - 1]" rather than "...[n]".

I'm a tiny bit surprised no one has organized this in terms
of a dictionary. I don't know, of course, how robust is the
characterization of n as a small integer. Maybe

lookup_table = {
0: "Replace",
1: "ChangeCase",
2: "Move"}

captures the sentiment; maybe something else does it better.
Surely for this requirement the *only* advantage of a dictionary over a
list is its ability to index with arbitrary values and thereby avoid the
need to use [n-1]. Wouldn't it therefore be less perverse to use

lookup_table = {
1: "Replace",
2: "ChangeCase",
3: "Move"}

Of course the dictionary would be a big win if the integer choice values
weren't a linear sequence. Otherwise using a list with a fixed offset is
likely to be quicker.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Mar 26 '07 #11
In article <ma***************************************@python. org>,
Steve Holden <st***@holdenweb.comwrote:
>Cameron Laird wrote:
>In article <ma***************************************@python. org>,
Jan Schilleman <ja************@xs4all.nlwrote:
>>Hi,

try this:
func = getattr(operations, ["Replace", "ChangeCase", "Move"][n])

HTH,
Jan

"ianaré" <ia****@gmail.comschreef in bericht
news:11**********************@p15g2000hsd.google groups.com...
yeah the subject doesn't really make sense does it?

anyway want I want to do is this:
if n == 1:

self.operations.insert(pos, operations.Replace.Panel(self, main))
.
.
.
I think you meant "...[n - 1]" rather than "...[n]".

I'm a tiny bit surprised no one has organized this in terms
of a dictionary. I don't know, of course, how robust is the
characterization of n as a small integer. Maybe

lookup_table = {
0: "Replace",
1: "ChangeCase",
2: "Move"}

captures the sentiment; maybe something else does it better.
Surely for this requirement the *only* advantage of a dictionary over a
list is its ability to index with arbitrary values and thereby avoid the
need to use [n-1]. Wouldn't it therefore be less perverse to use

lookup_table = {
1: "Replace",
2: "ChangeCase",
3: "Move"}

Of course the dictionary would be a big win if the integer choice values
weren't a linear sequence. Otherwise using a list with a fixed offset is
likely to be quicker.
Mar 26 '07 #12
On Mar 25, 6:36 pm, "ianaré" <ian...@gmail.comwrote:
yeah the subject doesn't really make sense does it?

anyway want I want to do is this:
if n == 1:

self.operations.insert(pos, operations.Replace.Panel(self, main))

elif n == 2:

self.operations.insert(pos, operations.ChangeCase.Panel(self,
main))

elif n == 3:

self.operations.insert(pos, operations.Move.Panel(self, main))

As you can see all the different functions have the same variables, so
it would be easier if I could just make a list and use that.

like this:

list = ["Replace", "ChangeCase", "Move"]
textVariable = list[n]
self.operations.insert(pos, operations.[textVariable].Panel(self,
main))
try this one:
textVariable = list[n-1]
exec( "self.operations.insert(pos, operations.%s.Panel(self, main))" %
textVariable )

Not sure if this is an elegant/right way.
-N

Mar 26 '07 #13

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

Similar topics

4
by: Fergus O'Shea | last post by:
I have some text that appears in a Database. This text includes HTML(e.g. <br> tags). I also have a Webpage that makes an XML call to that database. But when the text is displayed on the page,...
5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
5
by: Rob | last post by:
Help me, I'm just beginning with programming in Access 2000. I've tried the http://www.mvps.org/access/api/api0001.htm but it won't work in Access. What am i doing wrong. I don't have...
4
by: Dave | last post by:
I have a program that I've written a class for. I need to call the function in the program from the class. When I try to call the function I receive the error, the name xxx does not exist in the...
5
by: Kurt Van Campenhout | last post by:
Hi, I am trying to get/set Terminal server information in the active directory on a windows 2000 domain. Since the ADSI calls for TS don't work until W2K3, I need to do it myself. I'm fairly...
3
by: John A. Prejean | last post by:
This one has me stumped. I have a base form I am trying to wrap up, but I have one problem. In two functions I am opening a "record detail" form. I would like to keep the code in the base form...
2
by: khokimfang | last post by:
Hi All, I want to ask how to call form with only one variable in VB.Net. I have 3 form (Form1, Form2 and Form3) and 1 module. in the module i want to declare public variable to call form for ex:...
2
by: kokain | last post by:
I want to use a string to call a function. I will just post the code because I don't really know the terminology or how to program for that matter. I am using Visual Studio express. Public Class...
4
by: hmiller | last post by:
Hey there folks, I was wondering if there was a way to store a list of variables in a table and then call them one at a time in some loop method. Here's what I've got: A table "Tab Names"...
19
by: thisis | last post by:
Hi All, i have this.asp page: <script type="text/vbscript"> Function myFunc(val1ok, val2ok) ' do something ok myFunc = " return something ok" End Function </script>
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.