quantlab.core.component.config_to_dict#

quantlab.core.component.config_to_dict(config)[source]#

Return a config dataclass as a plain dict, components as their own configs.

Every declared component field (see component) is written as the get_config() of the component it holds: one dict, a list of them, or a dict of them keyed as the field is. Every other field is deep-copied, a nested dataclass written as dataclasses.asdict; a free-form dict is copied as data whatever keys it holds.

A component held at several places under config is written at each of them with a "shared_as" key, its first path as walk_components spells it, and rebuild turns those configs back into one object. The places are found once, by the outermost call; the get_config() calls nested in it use the same marks.

Parameters:
configdataclass instance

The config to write.

Returns:
dict

One entry per field, in field order.

Examples

With factor a factor over a StockDataset:

>>> saved = config_to_dict(factor.config)
>>> saved["dataset"]["name"]
'quantlab.dataset.stock.StockDataset'

With backtester whose price dataset is also its model’s first factor’s dataset:

>>> saved = config_to_dict(backtester.config)
>>> saved["price_dataset"]["shared_as"]
'price_dataset'
>>> saved["model"]["factors"][0]["dataset"]["shared_as"]
'price_dataset'