Is there a library for recursive object creation using config objects?

I would like to be able to create objects of classes which inherit from something like Configurable through a factory method in addition to the normal constructor, where the factory method takes some kind of config object which is limited to JSON-serializable data types.

So in addition to Class1(a=1, b="x") it should be possible to also do Class1.from_config(dict(a=1, b="x")).

This should work recursively so if the value of some initialization parameter is a configurable object it should get created from a nested config object through the from_config method, i.e. in addition to Class1(c=Class2()) it should be possible to do Class.from_config(dict(c=dict())`

Each configurable object should have a method to return the config object that can be used to create a clone, e.g. myobject.get_config().

My feeling is that this is such a basic approach that there should be many libraries already, but I could not really find one – could you point me to the right places?

The reason why I would like to find (or implement) this is because I would like to use this to duplicate my classes in other processes that way: send the config obejct of possibly nested complex classes to the other process to create a “twin” object to use there. One deeper reason for this is because that mechanism could then get extended to automatically handle the sharing of large datastructures when creating and using the config object to recreate twin objects in other processes.

We’ve been using factory-boy: factory-boy · PyPI

Pretty good indeed.

Cameron Simpson

1 Like

There is also the traitlets library which is used by the Jupyter stack to provide a typed configuration system (as well as event listeners).

1 Like

Thanks, this seems to do part of what I am after (and then a lot more).