traitlets
traitlets copied to clipboard
Nested configurables
We're looking to use traitlets to enforce type checking in the configuration of a coastal ocean model thetis (github.com/thetisproject/thetis).
Right now, we have a tree of configuration options. For example:
ModelOptions:
turbulence_model = "some_value"
turbulence_model_options = TurbulenceModelOptions
Right now, we just use nested dictionaries for this.
It seems like one would like to have a Configurable with traits that are themselves Configurable objects:
right now we use:
class TurbulenceModelOptions(Configurable):
pass
class GLSOptions(TurbulenceModelOptions):
use_hyperviscosity = Bool()
class SmagorinskyOptions(TurbulenceModelOptions):
background_viscosity = Float()
class ModelOptions(Configurable):
turbulence_model = Enum(["gls", "smagorinsky"])
turbulence_model_options = Instance(TurbulenceModelOptions)
But generating (for example) rST doesn't recurse into the trait values, so the presentation of the options isn't right.
Are we doing this wrong? Is there some more obvious way of glueing together such a hierarchy?
To generate rst options, you would need the flat list of configurable classes, and include them all. To pass the configuration to your members, you should make sure to instantiate them with parent=self or config=self.config, to ensure that configuration propagates all the way down.
@wence- have you tried to make update_config() to work with a true hierarchical model of values?
I mean to be able to specify different trait-values for SubModel class depending on whether it is attached as a child of FooModel or BarModel, like that:
c.FooModel.sub_model.trait_a = 1
c.BarModel.sub_model.trait_a = 2