0

I have a code that looks something like this:

def generate_constraints(c):
    if c.name == 'multibodypendulum':
        con_fnc = MultiBodyPendulum(**c)

where c is an OmegaConf object containing a bunch of parameters. Now I would like to pass all the parameters in c except the name to the MultiBodyPendulum class (the name was used to identify the class to send the rest of the parameters to). However I have no idea how to make a copy of c without one parameter. Does anyone have a good solution for this?

Tue
  • 371
  • 1
  • 14

2 Answers2

2

I'd recommend using DictConfig.copy to create a copy and del to delete attributes/items from the DictConfig object.

config: DictConfig = ...

config_copy = config.copy()
del config_copy.unwanted_parameter
pendulum = MultiBodyPendulum(**config_copy)
Jasha
  • 5,507
  • 2
  • 33
  • 44
1

You could try to get a dictionary from the instance using my_dict = c.__dict__ and then remove the class from the dictionary using pop()

something like:

my_dict = c.__dict__
old_name = my_dict.pop("name", None)
con_fnc = MultiBodyPendulum(**my_dict)

That way you would add every parameter except for the name, if you wanted to add a different name you can add it to the new instance creation like:

con_fnc = MultiBodyPendulum(name="New Name", **my_dict)

Edit:

To conserve the OmegaConf functionality use:

my_dict = OmegaConf.to_container(c)
Sergio García
  • 486
  • 5
  • 15