credmark.cmf.model.ImmutableModel

class ImmutableModel(context)[source]

Bases: BaseModel

Subclass with ImmutableModel for models whose result is only available after a particular block and will not change for the future block numbers.

It shall return a derived class of ImmutableOutput with field firstResultBlockNumber set.

For blocks before which data is unavailable, the model should throw ModelDataError. Behind the scenes it uses CachePolicy.IMMUTABLE cache.

Methods

convert_dict_to_dto

A model can call this method to convert a dict of data in a known format into a DTO instance.

describe

Decorator for credmark.cmf.model.ImmutableModel subclasses to describe the model.

init

Subclasses may override this method to do any model instance initiation.

run

Attributes

Parameters

context (ModelContext) –

convert_dict_to_dto(data, dto_class)

A model can call this method to convert a dict of data in a known format into a DTO instance.

Parameters
classmethod describe(*, slug, version, display_name=None, description=None, developer=None, category=None, subcategory=None, tags=None, input=<class 'credmark.dto.EmptyInput'>, output=<class 'credmark.cmf.types.series.ImmutableOutput'>, errors=None)[source]

Decorator for credmark.cmf.model.ImmutableModel subclasses to describe the model.

  1. Immutable model shall raise an error if result is not available before a block

``` if self.context.block_number < 100:

raise ModelDataError(‘Block number shall be greater than 100’)

```

  1. Immutable model shall return the type of ImmutableOutput with the field firstResultBlockNumber.

Example usage:

from credmark.cmf.model import ImmutableModel
from credmark.cmf.types.series import ImmutableOutput

@ImmutableModel.describe(slug='example.echo-imm',
                version='1.0',
                display_name='Echo',
                description="A test model to echo the message property sent in input.",
                developer="my_username",
                category="financial",
                input=EchoDto,
                output=ImmutableOutput)
class EchoModel(ImmutableModel):
    def run(self, input: EchoDto, from_block: BlockNumber) -> ImmutableOutput:
        return ImmutableOutput(
        ...
Parameters
  • slug (str) – slug (short unique name) for the model. Can contain alpha-numeric and and underscores. Contributor slugs must start with "contrib." Once submitted, the model slug cannot be changed.

  • version (str) – version string, ex. "1.0". The version number can be incremented when the model code is updated.

  • display_name (Optional[str]) – Name for the model

  • description (Optional[str]) – Description of the model. If description is not set, the doc string (__doc__) of the model class is used instead.

  • developer (Optional[str]) – Name or nickname of the developer

  • category (Optional[str]) – Category of the model (ex. “financial”, “protocol” etc.)

  • subcategory (Optional[str]) – Optional subcategory (ex. “aave”)

  • tags (Optional[list[str]]) – optional list of string tags describing the model

  • input (Union[Type[Union[BaseModel, IntDTO, StrDTO, FloatDTO]], Type[dict]]) – Type that model uses as input; a DTO subclass or dict. Defaults to EmptyInput object.

  • output (Optional[Type[ImmutableOutput]]) – Type that the model run returns; a ImmutableOutput subclass.

  • errors (Optional[Union[List[ModelErrorDesc], ModelErrorDesc]]) – If the model raises ModelDataError, set this to a configured ModelDataErrorDesc instance (or a list of instances) describing the errors. Defaults to None.

init()

Subclasses may override this method to do any model instance initiation.