Convert the contents of a string into name of variable

E

Erwan VITIERE

Hello,
I want to convert the contents of a string into name of variable.
For example:

var1="toto"
....
toto=5
print toto
 
F

Fredrik Lundh

Erwan said:
I want to convert the contents of a string into name of variable.
For example:

var1="toto"
...
toto=5
print toto

why?

Python works better if you use it to write Python code. the Python
solution is to use a dictionary:

key1 = "toto"

data = {}
data["toto"] = 5

print data[key1]

</F>
 
E

Erwan VITIERE

Because i don't want to use this syntax because it is too long, I want a
direct access :

Not :
DicoUser['TOTO'].DicoTable.['MY_TABLE'].DicoLabel.['MY_LABEL'] = "for
exemple"

Finally, i want to use :
TOTO.MY_TABLE.MY_LABEL = "for exemple"

Fredrik Lundh said:
Erwan said:
I want to convert the contents of a string into name of variable.
For example:

var1="toto"
...
toto=5
print toto

why?

Python works better if you use it to write Python code. the Python
solution is to use a dictionary:

key1 = "toto"

data = {}
data["toto"] = 5

print data[key1]

</F>
 
3

3KWA

Erwan said:
Because i don't want to use this syntax because it is too long, I want a
direct access :

Not :
DicoUser['TOTO'].DicoTable.['MY_TABLE'].DicoLabel.['MY_LABEL'] = "for
exemple"

Finally, i want to use :
TOTO.MY_TABLE.MY_LABEL = "for exemple"

setattr might be what you are looking for
 
D

Dennis Lee Bieber

Because i don't want to use this syntax because it is too long, I want a
direct access :

Not :
DicoUser['TOTO'].DicoTable.['MY_TABLE'].DicoLabel.['MY_LABEL'] = "for
exemple"

Finally, i want to use :
TOTO.MY_TABLE.MY_LABEL = "for exemple"
What's with the dotted notation? Looks like a dialect of REXX's
stem variables. Though even those do not do variable translation for the
first item -- you couldn't do

aUser = "TOTO"
aUser.MY_TABLE.MY_LABEL = ...

Yes, I know Python has dotted notation, but for accessing
members of modules or class instances, and I'd be leery of any code that
was using string variables to access into those.

What sort of data structure are you trying to access?

-=-=-=-=-=-=-=-

# need to initialize some test data first
tLabel = {"MY_LABEL" : "Some junk",
"OTHER LABEL" : "Other junk"}

tTable = {"MY_TABLE" : tLabel,
"OTHER_TABLE" : {"THIRD LABEL" : "More junk"} }

Users = {"TOTO" : tTable}

print

# Okay, put the strings into variables
aUser = "TOTO"
aTable = "MY_TABLE"
aLabel = "MY_LABEL"

# access something in the data
print Users[aUser][aTable][aLabel]

# change something in the data
Users[aUser][aTable][aLabel] = "For Example"

# show the whole structure
print Users

#access the changed item, using literals and variables mixed
print Users["TOTO"][aTable]["MY_LABEL"]

-=-=-=-=-=-=-=-=-

Some junk
{'TOTO': {'OTHER_TABLE': {'THIRD LABEL': 'More junk'}, 'MY_TABLE':
{'MY_LABEL': 'For Example', 'OTHER LABEL': 'Other junk'}}}
For Example

The REXX equivalent would look something like (I've not done
REXX in a decade):

Users."TOTO"."MY_TABLE"."MY_LABEL" = "Some junk"
Users."TOTO"."MY_TABLE"."OTHER LABEL" = "Other junk"
Users."TOTO". "OTHER_TABLE"."THIRD LABEL" = "More junk"

aUser = "TOTO"
aTable = "MY_TABLE"
aLabel = "MY_LABEL"

say Users.aUser.aTable.aLabel

Users.aUser.aTable.aLabel = "For example"

/* no equivalent for printing the entire structure */

say Users."TOTO".aTable."MY_LABEL"

--
 
E

elbertlev

TRy db_row does exactly what you want to do. Slower, but more simple:
##############################
#Sequence2Struct.py

class Struct:
pass

def MakeStruct(seq, names):
obj = Struct()
if len(seq) != len(names):
raise IndexError("seq and names are not the same length")
for i in range(len(names)):
obj.__dict__[names] = seq
return obj

def ExtractNames(t):
return [item[0] for item in t]


if __name__ == "__main__":
t = (1, 2, 3)
n1 = (("A", 1), ("B", 1), ("C", 2))
n = ExtractNames(n1)
print n
s = MakeStruct(t, n)
print s, s.A, s.B, s.C
n = ("A", "B")
s1 = MakeStruct(t, n)
##############################
 
B

Bruno Desthuilliers

Erwan VITIERE a écrit :
Hello,
I want to convert the contents of a string into name of variable.
For example:

var1="toto"
...
toto=5
print toto

exec "toto = 5"
print toto

But I would use another solution if possible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top