在我的代码中,我需要Dict
中的Instance
(例如,按名称键入的Parameter
列表)。目前,我已经通过使用常规的Dict
-traitlet作为传入属性(parameters
)并具有一个将这些属性“转换”为Parameter
类实例的函数来解决此问题。 / p>
有没有比这更好的方法了?
import traitlets as t
import traitlets.config as tc
class Parameter(tc.Configurable):
name = t.Unicode().tag(config=True)
description = t.Unicode(allow_none=True).tag(config=True)
value = t.Any(default_value=None).tag(config=True)
class Job(tc.Configurable):
parameters = t.Dict(allow_none=True).tag(config=True)
_parameter_map = t.Dict()
def init_parameters(self):
self._parameter_map.clear()
for name, configuration in self.parameters.items():
configuration['name'] = name
parameter = Parameter(**configuration, parent=self)
self._parameter_map[name] = parameter
然后这个:
c.Job.parameters = {
"parameter1": {
"description": "The first parameter",
"value": True
}
}
它的工作原理和逻辑决定了-由于您是使用特征集通过“类名”进行配置的-这是唯一的方法,但我只是想确定