call to function by text variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?iso-8859-1?B?aWFuYXLp?=

    #1

    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.Repl ace.Panel(self, main))

    elif n == 2:

    self.operations .insert(pos, operations.Chan geCase.Panel(se lf,
    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

  • =?ISO-8859-2?Q?Wojciech_Mu=B3a?=

    #2
    Re: call to function by text variable

    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(operati ons, textVariable).P anel(self.main)
    )

    Comment

    • Steve Holden

      #3
      Re: call to function by text variable

      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.Repl ace.Panel(self, main))
      >
      elif n == 2:
      >
      self.operations .insert(pos, operations.Chan geCase.Panel(se lf,
      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.Repl ace,
      operations.Chan geCase,
      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

      Comment

      • Steve Holden

        #4
        Re: call to function by text variable

        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.Repl ace.Panel(self, main))
        >
        elif n == 2:
        >
        self.operations .insert(pos, operations.Chan geCase.Panel(se lf,
        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.Repl ace,
        operations.Chan geCase,
        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

        Comment

        • Gabriel Genellina

          #5
          Re: call to function by text variable

          En Sun, 25 Mar 2007 19:36:26 -0300, ianaré <ianare@gmail.c omescribió:
          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(operati ons, textVariable) == operations.Repl ace

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

          --
          Gabriel Genellina

          Comment

          • attn.steven.kuo@gmail.com

            #6
            Re: call to function by text variable

            On Mar 25, 3:36 pm, "ianaré" <ian...@gmail.c omwrote:
            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.Repl ace.Panel(self, main))
            >
            elif n == 2:
            >
            self.operations .insert(pos, operations.Chan geCase.Panel(se lf,
            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.Repl ace.Panel,
            operations.Chan ge.Panel,
            operations.Move .Panel]


            # and invocation would be:

            self.operations .insert(pos, unbound_funcs[n - 1](self, main))


            --
            Hope this helps,
            Steven


            Comment

            • Jan Schilleman

              #7
              Re: call to function by text variable

              Hi,

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

              HTH,
              Jan

              "ianaré" <ianare@gmail.c omschreef in bericht
              news:1174862186 .134912.117270@ p15g2000hsd.goo glegroups.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.Repl ace.Panel(self, main))
              >
              elif n == 2:
              >
              self.operations .insert(pos, operations.Chan geCase.Panel(se lf,
              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
              >
              --

              >


              Comment

              • =?iso-8859-1?B?aWFuYXLp?=

                #8
                Re: call to function by text variable

                Cool now I can run it through the translator.

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


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

                Thanks guys!

                Comment

                • =?iso-8859-1?B?aWFuYXLp?=

                  #9
                  Re: call to function by text variable

                  On Mar 25, 7:01 pm, "ianaré" <ian...@gmail.c omwrote:
                  Cool now I can run it through the translator.
                  >
                  ops = (_("Directory") , _("Replace"), _("ChangeCase") ,
                  _("Move"), _("Swap"), _("Insert"), _("ChangeLength "))
                  >
                  self.operations .insert(pos, getattr(operati ons, ops[n]).Panel(self,
                  main))
                  >
                  Thanks guys!
                  erm ... brainfart LOL. But now I can link it to the translated list.

                  Comment

                  • Cameron Laird

                    #10
                    Re: call to function by text variable

                    In article <mailman.5604.1 174863631.32031 .python-list@python.org >,
                    Jan Schilleman <jan.schilleman @xs4all.nlwrote :
                    >Hi,
                    >
                    >try this:
                    >func = getattr(operati ons, ["Replace", "ChangeCase ", "Move"][n])
                    >
                    >HTH,
                    >Jan
                    >
                    >"ianaré" <ianare@gmail.c omschreef in bericht
                    >news:117486218 6.134912.117270 @p15g2000hsd.go oglegroups.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.Repl ace.Panel(self, main))

                    Comment

                    • Steve Holden

                      #11
                      Re: call to function by text variable

                      Cameron Laird wrote:
                      In article <mailman.5604.1 174863631.32031 .python-list@python.org >,
                      Jan Schilleman <jan.schilleman @xs4all.nlwrote :
                      >Hi,
                      >>
                      >try this:
                      >func = getattr(operati ons, ["Replace", "ChangeCase ", "Move"][n])
                      >>
                      >HTH,
                      >Jan
                      >>
                      >"ianaré" <ianare@gmail.c omschreef in bericht
                      >news:117486218 6.134912.117270 @p15g2000hsd.go oglegroups.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.Repl ace.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
                      characterizatio n 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

                      Comment

                      • Cameron Laird

                        #12
                        Re: call to function by text variable

                        In article <mailman.5612.1 174891359.32031 .python-list@python.org >,
                        Steve Holden <steve@holdenwe b.comwrote:
                        >Cameron Laird wrote:
                        >In article <mailman.5604.1 174863631.32031 .python-list@python.org >,
                        >Jan Schilleman <jan.schilleman @xs4all.nlwrote :
                        >>Hi,
                        >>>
                        >>try this:
                        >>func = getattr(operati ons, ["Replace", "ChangeCase ", "Move"][n])
                        >>>
                        >>HTH,
                        >>Jan
                        >>>
                        >>"ianaré" <ianare@gmail.c omschreef in bericht
                        >>news:11748621 86.134912.11727 0@p15g2000hsd.g ooglegroups.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.Repl ace.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
                        >characterizati on 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.

                        Comment

                        • Nanjundi

                          #13
                          Re: call to function by text variable

                          On Mar 25, 6:36 pm, "ianaré" <ian...@gmail.c omwrote:
                          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.Repl ace.Panel(self, main))
                          >
                          elif n == 2:
                          >
                          self.operations .insert(pos, operations.Chan geCase.Panel(se lf,
                          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.operation s.insert(pos, operations.%s.P anel(self, main))" %
                          textVariable )

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



                          Comment

                          Working...