0
I have a dictionary that contains the name of the activities and some characteristics of this activity.
I created a class called process and I want to iterate in my dictionary creating the processes.
process_name = ['processo1', 'processo2', 'processo3']
process_data = {
'processo1': {
'prod': 1.2,
'maquinario': 'maq1'
},
'processo2': {
'prod': 0.7,
'maquinario': 'maq2'
},
'processo3': {
'prod': 0.9,
'maquinario': 'maq3'
}
}
# ______________________________
class Process(object):
def __init__(self , dictionary):
for key in dictionary:
setattr(self, key, dictionary[key])
# ______________________________
for name in process_name:
processo = Process(process_data[name])
name = processo
He’s creating the object with the name name
or process, and not with the name of the respective process.
How can I fix this? Thank you!
Dude, I don’t understand exactly what you expect from the code. You want it to save the name of the process as well (which in this case would be
processo1, etc..
) ???– fernandosavio
face, I want it to create the process object 1,... , so I can have the process as a class, and be able to access its parameters, like: process1.Prod and etc
– Willi Gerber
what it is returning to me is name.Prod, instead of process1.Prod, for example
– Willi Gerber
Hmm.. You want the variable name to be the same as in the process. I don’t see the need for this, but if you really need to have the same name why not create a dictionary and use the name as the key and the object as the key?
– fernandosavio
I don’t understand. 1 - if I don’t put the variable name as the process name, how will I call the specific productivity of a process in the code? , as it is there, it is returning the last element of the list, in this case the process3 (name.Prod). I can’t access process1.Prod, for example 2 - I don’t understand the proposed solution
– Willi Gerber
Actually what I proposed is what you’ve tried to do on the line with the code
name = processo
, but this does not alter the original dictionary. I will post an answer with an example and see if it solves your problem– fernandosavio
blz!! thank you!
– Willi Gerber