Formatted string to object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • janama

    #1

    Formatted string to object

    Hi,

    can such a thing be done somehow?


    aaa = self.aaa
    bbb = %s.%s % ('parent', 'bbb')

    Can you use strings or %s strings like in the above or

    aaa = 'string'
    aaa.%s() % 'upper'

    Somehow?

    Thanks for taking a look at this

    Regards
    Janama

  • Tim Chase

    #2
    Re: Formatted string to object

    > Can you use strings or %s strings like in the above or[color=blue]
    >
    > aaa = 'string'
    > aaa.%s() % 'upper'
    >
    > Somehow?[/color]

    Looks like you want to play with the eval() function.
    [color=blue][color=green][color=darkred]
    >>> aaa = 'hello'
    >>> result = eval("aaa.%s()" % 'upper')
    >>> result[/color][/color][/color]
    'HELLO'

    Works for your second example. May work on your first example too.

    -tkc



    Comment

    • bruno at modulix

      #3
      Re: Formatted string to object

      Tim Chase wrote:[color=blue][color=green]
      >> Can you use strings or %s strings like in the above or
      >>
      >> aaa = 'string'
      >> aaa.%s() % 'upper'
      >>
      >> Somehow?[/color]
      >
      >
      > Looks like you want to play with the eval() function.
      >[color=green][color=darkred]
      >>>> aaa = 'hello'
      >>>> result = eval("aaa.%s()" % 'upper')
      >>>> result[/color][/color]
      > 'HELLO'[/color]

      Using eval() or exec should be really avoided whenever possible IMHO.
      This one is best done with getattr(obj, attribute_name [,default]), ie:
      [color=blue][color=green][color=darkred]
      >>> aaa = "string"
      >>> getattr(aaa, 'upper')()[/color][/color][/color]
      'STRING'[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]


      --
      bruno desthuilliers
      python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
      p in 'onurb@xiludom. gro'.split('@')])"

      Comment

      • Steven Bethard

        #4
        Re: Formatted string to object

        janama wrote:[color=blue]
        > can such a thing be done somehow?
        >
        > aaa = self.aaa
        > bbb = %s.%s % ('parent', 'bbb')
        >
        > Can you use strings or %s strings like in the above or
        >
        > aaa = 'string'
        > aaa.%s() % 'upper'[/color]

        Use the getattr() function::
        [color=blue][color=green][color=darkred]
        >>> class parent(object):[/color][/color][/color]
        ... class bbb(object):
        ... pass
        ...[color=blue][color=green][color=darkred]
        >>> module_ns = __import__(__na me__)
        >>> getattr(getattr (module_ns, 'parent'), 'bbb')[/color][/color][/color]
        <class '__main__.bbb'>[color=blue][color=green][color=darkred]
        >>> import string
        >>> getattr(getattr (module_ns, 'string'), 'upper')[/color][/color][/color]
        <function upper at 0x00B502B0>

        I've imported the module itself here so you can use getattr, but you
        could also use globals() instead of the inner getattr() call::
        [color=blue][color=green][color=darkred]
        >>> getattr(globals ()['parent'], 'bbb')[/color][/color][/color]
        <class '__main__.bbb'>[color=blue][color=green][color=darkred]
        >>> getattr(globals ()['string'], 'upper')[/color][/color][/color]
        <function upper at 0x00B502B0>

        HTH,

        STeVe

        Comment

        • bruno at modulix

          #5
          Re: Formatted string to object

          janama wrote:[color=blue]
          > Hi,
          >
          > can such a thing be done somehow?
          >
          >
          > aaa = self.aaa
          > bbb = %s.%s % ('parent', 'bbb')[/color]

          Given the first line, I assume this is inside a method body, and parent
          is a local var. Then the answer is:

          bbb = getattr(locals( )['parent'], 'bbb')

          read the doc for these two functions to get more infos.
          [color=blue]
          > Can you use strings or %s strings like in the above or
          >
          > aaa = 'string'
          > aaa.%s() % 'upper'
          >
          > Somehow?[/color]

          Not directly - but here again, getattr() is your friend.


          --
          bruno desthuilliers
          python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
          p in 'onurb@xiludom. gro'.split('@')])"

          Comment

          • janama

            #6
            Re: Formatted string to object

            Thankyou everyone for help last time:

            The following works ok when setting a
            wx.lib.buttons. GenBitmapTextBu tton's disabled bitmap in using wxpython

            instead of this:
            self.b1.SetBitm apDisabled(self .yellow)

            i can use this:
            aaa = 1
            result1 = eval("self.b%s. SetBitmapDisabl ed(self.yellow) " % aaa)
            result

            YAY!


            How would i to achieve this with getattr() ?

            getattr(locals( )['parent'], aaa) ???

            Why would this be better as recommended in previous post here



            Any help appreciated

            ta

            Comment

            • Scott David Daniels

              #7
              Re: Formatted string to object

              janama wrote:[color=blue]
              > i can use this:
              > aaa = 1
              > result = eval("self.b%s. SetBitmapDisabl ed(self.yellow) " % aaa)
              >
              > .... How would i to achieve this with getattr() ?[/color]

              result = getattr(self, 'b%s' % aaa).SetBitmapD isabled(self.ye llow)

              --Scott David Daniels
              scott.daniels@a cm.org

              Comment

              Working...