credmark.dto.transform.transform_data_for_dto

transform_data_for_dto(data, dto_class, slug, data_source)[source]

Transform data (dict, DTO or None) to a DTO class or a dict. This can be used to ensure you have the DTO (or dict) you expect. Specifying dto_class=None will convert data to a dict.

It can also be used to create DTOs from model output that might be generic, for example models that call other models.

Note that if you have a data structure such as a dict or list that contains DTOs, the transformation is not deep so the embedded values will remain as DTOs. In this case, to serialize the data you can use credmark.dto.json_dump or json_dumps.

Parameters
  • data (Optional[Union[dict, BaseModel, IntDTO, StrDTO, FloatDTO]]) – data in the form of a dict or DTO instance

  • dto_class (Optional[Type[Union[BaseModel, IntDTO, StrDTO, FloatDTO]]]) – DTO subclass (or None to convert to dict)

  • slug (str) – the slug of the calling model (used only for error messages)

  • data_source (str) – short string describing source of data, ex “input” (used for error messages)