Python Forum

Full Version: Is there a library for recursive object creation using config objects
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
 o1 = Class1(c=Class2())
it should be possible to do
 o1 = 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.
o1.get_config() == dict(c=dict())
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.