Convert dictionaries to class

Asked

Viewed 37 times

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..) ???

  • 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

  • what it is returning to me is name.Prod, instead of process1.Prod, for example

  • 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?

  • 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

  • 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

  • blz!! thank you!

Show 2 more comments

1 answer

0


The problem lies within the for in attributing to nome

for name in process_name:
    processo = Process(process_data[name])
    name = processo  # <------

The variable name is assigned correctly (although it does not have the effect you want) but is overwritten in the next loop iteration.

So you can access the Process by name just replace:

    name = processo

For:

    process_data[name] = processo

So the changes will be modified in the original dictionary.

Below is an example of your modified code:

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])
    # única alteração no código
    process_data[name] = processo

# Dicionário atualizado com objetos do tipo Process
print(process_data)

# Acesso aos Process por nome
print(process_data['processo1'])

Code working

  • Now yes I understood what you tried to say before there. Thank you very much!

  • I misspelled that comment.. I wrote to put the name with key and the process as key was meant to be value. :)

  • If @fernandosavio’s answer solved your problem and resolved your questions mark it as correct so that it can in the future help others who have the same doubt.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.