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

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

Jun 19 '06 #1
6 1234
> Can you use strings or %s strings like in the above or

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

Somehow?


Looks like you want to play with the eval() function.
aaa = 'hello'
result = eval("aaa.%s()" % 'upper')
result

'HELLO'

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

-tkc

Jun 19 '06 #2
Tim Chase wrote:
Can you use strings or %s strings like in the above or

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

Somehow?

Looks like you want to play with the eval() function.
aaa = 'hello'
result = eval("aaa.%s()" % 'upper')
result 'HELLO'


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

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jun 19 '06 #3
janama wrote:
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'


Use the getattr() function::
class parent(object): ... class bbb(object):
... pass
... module_ns = __import__(__name__)
getattr(getattr(module_ns, 'parent'), 'bbb') <class '__main__.bbb'> import string
getattr(getattr(module_ns, 'string'), 'upper') <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::
getattr(globals()['parent'], 'bbb') <class '__main__.bbb'> getattr(globals()['string'], 'upper')

<function upper at 0x00B502B0>

HTH,

STeVe
Jun 19 '06 #4
janama wrote:
Hi,

can such a thing be done somehow?
aaa = self.aaa
bbb = %s.%s % ('parent', 'bbb')
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.
Can you use strings or %s strings like in the above or

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

Somehow?


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 'o****@xiludom.gro'.split('@')])"
Jun 19 '06 #5
Thankyou everyone for help last time:

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

instead of this:
self.b1.SetBitmapDisabled(self.yellow)

i can use this:
aaa = 1
result1 = eval("self.b%s.SetBitmapDisabled(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

http://groups.google.com.au/group/co...f080b21d668081

Any help appreciated

ta

Jun 26 '06 #6
janama wrote:
i can use this:
aaa = 1
result = eval("self.b%s.SetBitmapDisabled(self.yellow)" % aaa)

.... How would i to achieve this with getattr() ?


result = getattr(self, 'b%s' % aaa).SetBitmapDisabled(self.yellow)

--Scott David Daniels
sc***********@acm.org
Jun 26 '06 #7

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

Similar topics

7
by: Thomas Philips | last post by:
I want to print "1 spam 4 you" using a formatted string that gets its inputs from the dictionary d={'n1':1, 's1':'spam', 'n2':4}. To do so, I write >>> x="%('n1')d %('s1')s %('n2')d you" >>> x...
4
by: Joe C | last post by:
I've written a console application and would like to isolate all screen output so that it will be easier to migrate the code to a GUI-type platform without modification to the base code. As a...
4
by: DazedAndConfused | last post by:
I encryted a serialized binary formatted object. Now I can't figure out how to deserialize it so that I can decrypt it. I used this code encrypt and write it out: Dim fe As New...
3
by: funkyMonkey | last post by:
I'm binding date fields from entity objects and formatting the output in the text box in short date format. Code: BindDateField(Me.txtCheckIn, "Text", reservation.BookingDetail, "CheckIn")...
1
by: Tarun Mistry | last post by:
Hi everyone, is it possible to return the formatted interpretation of an XmlDocument object? Using an XmlWriter object I can write a formatted document to a file, however I want to store it...
2
by: JSheble | last post by:
I have a method in my class that needs to return formatted XML, with the carriage returns, linefeeds, and tabs... However, when I return oXml.OuterXml, the Xml is not formatted... Every example...
1
by: Joe | last post by:
I want to save some URLs into a XML formatted document. > > I find out that its having some problems due to some of the characters > used > in the URL. > > Is there a quick way to get around...
0
by: satarupa | last post by:
I want to read the formatted (bold,underline,italics) text of a word dcoument using c# i can read the document even the paragraphn in it but not the formatted text plzz help following is the...
8
by: Andy B | last post by:
I have the object property StockContract.Dictionary which is a dictionary collection of <string, stringkey/value pairs. I need to be able to retreive the keys and their values and display them on a...
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
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
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...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.