faust.models.base

Model descriptions.

The model describes the components of a data structure, kind of like a struct in C, but there’s no limitation of what type of data structure the model is, or what it’s used for.

A record (faust.models.record) is a model type that serialize into dictionaries, so the model describe the fields, and their types:

>>> class Point(Record):
...    x: int
...    y: int

>>> p = Point(10, 3)
>>> assert p.x == 10
>>> assert p.y == 3
>>> p
<Point: x=10, y=3>
>>> payload = p.dumps(serializer='json')
'{"x": 10, "y": 3, "__faust": {"ns": "__main__.Point"}}'
>>> p2 = Record.loads(payload)
>>> p2
<Point: x=10, y=3>

Models are mainly used for describing the data in messages: both keys and values can be described as models.

faust.models.base.registry = {'@ClientAssignment': <class 'faust.assignor.client_assignment.ClientAssignment'>, '@ClientMetadata': <class 'faust.assignor.client_assignment.ClientMetadata'>, '@ClusterAssignment': <class 'faust.assignor.cluster_assignment.ClusterAssignment'>, '@ReqRepRequest': <class 'faust.agents.models.ReqRepRequest'>, '@ReqRepResponse': <class 'faust.agents.models.ReqRepResponse'>, '@TableInfo': <class 'faust.web.apps.tables.TableInfo'>}

Global map of namespace -> Model, used to find model classes by name. Every single model defined is added here automatically when a model class is defined.

class faust.models.base.Model(*args, **kwargs) → None[source]

Meta description model for serialization.

classmethod loads(s: bytes, *, default_serializer: Union[faust.types.codecs.CodecT, str, NoneType] = None) → faust.types.models.ModelT[source]

Deserialize model object from bytes.

Parameters:
  • default_serializer (CodecArg) – Default serializer to use if no custom serializer was set for this model subclass.
  • **kwargs – Additional attributes to set on the model object. Note, these are regarded as defaults, and any fields also present in the message takes precedence.
Return type:

ModelT

to_representation() → Any[source]

Convert object to JSON serializable object.

Return type:Any
derive(*objects, **fields) → faust.types.models.ModelT[source]
Return type:ModelT
dumps(*, serializer: Union[faust.types.codecs.CodecT, str, NoneType] = None) → bytes[source]

Serialize object to the target serialization format.

Return type:bytes
class faust.models.base.FieldDescriptor(field: str, type: Type, model: Type[faust.types.models.ModelT], required: bool = True, default: Any = None, parent: faust.types.models.FieldDescriptorT = None) → None[source]

Describes a field.

Used for every field in Record so that they can be used in join’s /group_by etc.

Examples

>>> class Withdrawal(Record):
...    account_id: str
...    amount: float = 0.0
>>> Withdrawal.account_id
<FieldDescriptor: Withdrawal.account_id: str>
>>> Withdrawal.amount
<FieldDescriptor: Withdrawal.amount: float = 0.0>
Parameters:
  • field (str) – Name of field.
  • type (Type) – Field value type.
  • model (Type) – Model class the field belongs to.
  • required (bool) – Set to false if field is optional.
  • default (Any) – Default value when required=False.
field = None

Name of attribute on Model.

type = None

Type of value (e.g. int, or Optional[int])).

model = None

The model class this field is associated with.

required = True

Set if a value for this field is required (cannot be None).

default = None

Default value for non-required field.

getattr(obj: faust.types.models.ModelT) → Any[source]
Return type:Any
ident