i'm working up a test to populate multiple object's various parameters from a list that comes from an sqlite3 table.
this is the code and the errors:
class thing(object):
def __init__(self, data):
self.name = data[0]
self.spoot = data[1]
self.lurmz = data[2]
def __str__(self):
output = f'{self.name} data → spoot: {self.spoot}, lurmz: {self.lurmz}'
return output
blorp_one = thing(['flarn', 750, 110])
blorp_two = thing(['gleep', 500, 70])
print(blorp_one) #<-- flarn data → spoot: 750, lurmz: 110
print(blorp_two) #<-- flarn data → spoot: 500, lurmz: 70
# data from sqlite3 db
result = [['blorp_one', 'spoot', 3750], ['blorp_one', 'lurmz', 610],
['blorp_two', 'spoot', 1250], ['blorp_two', 'lurmz', 660]]
result[0][0].result[0][1] = result[0][2]
result[1][0].result[1][1] = result[1][2]
result[2][0].result[2][1] = result[2][2]
result[3][0].result[3][1] = result[3][2]
# each one → AttributeError: 'str' object has no attribute 'result'
print(blorp_one) #<-- desired output: flarn data → spoot: 3750, lurmz: 610
print(blorp_two) #<-- desired output: flarn data → spoot: 1250, lurmz: 660
and i've tried these to no avail:
tmp1 = eval(f'{result[0][0]'})
tmp1.result[0][1] = result[0][2] #<-- object has no attribute 'result'
tmp2 = eval(f'{result[0][1]'})
tpm1.tmp2 = result[0][2] #<-- ?? doesn't error out, but doesn't change the params
eval(f'{result[0][0]'}).result[0][1] #<-- nope. assignment error
now i know it's going to be something simple, but i'm not seeing it (maybe because it's Friday?). can someone kindly re-orient my brain? thanks.
EDIT: i understand that strings are immutable. but i don't know how to take the stored object NAME, which is a string, and use it to assign new values back to it's parameters; in effect use the string to refer to an object... this seems pretty bog-standard stuff, but for some reason, it's just not clicking in my head.
How about this:
# no changes here:
class thing(object):
def __init__(self, data):
self.name = data[0]
self.spoot = data[1]
self.lurmz = data[2]
def __str__(self):
output = f'{self.name} data → spoot: {self.spoot}, lurmz: {self.lurmz}'
return output
blorp_one = thing(['flarn', 750, 110])
blorp_two = thing(['gleep', 500, 70])
print(blorp_one) #<-- flarn data → spoot: 750, lurmz: 110
print(blorp_two) #<-- flarn data → spoot: 500, lurmz: 70
# data from sqlite3 db
result = [
['blorp_one', 'spoot', 3750],
['blorp_one', 'lurmz', 610],
['blorp_two', 'spoot', 1250],
['blorp_two', 'lurmz', 660]
]
# but changes here:
blorps = {
'blorp_one': blorp_one,
'blorp_two': blorp_two,
}
for result in results:
blorp_name = result[0]
blorp_attribute = result[1]
blorp_value = result[2]
# could also say:
# blorp_name, blorp_attribute, blorp_value = result
the_blorp = blorps[blorp_name]
setattr(the_blorp, blorp_attribute, blorp_value)
print(blorp_one) #<-- should output: flarn data → spoot: 3750, lurmz: 610
print(blorp_two) #<-- should output: flarn data → spoot: 1250, lurmz: 660