Skip to content

Rich Representation Mixin

RichReprMixin

Bases: BaseModel

Mixin to limit the length of values in the rich representation of a Pydantic model. This is useful for large objects that should not be displayed in their entirety in the rich representation.

Set MAX_CHARS_PER_VALUE to the maximum number of characters to display for each value. If a value exceeds this limit, it will be displayed as <object of length: {len(str(value))}>.

Source code in src/edgygraph/rich.py
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class RichReprMixin(BaseModel):
    """
    Mixin to limit the length of values in the rich representation of a Pydantic model.
    This is useful for large objects that should not be displayed in their entirety in the rich representation.

    Set MAX_CHARS_PER_VALUE to the maximum number of characters to display for each value.
    If a value exceeds this limit, it will be displayed as ```<object of length: {len(str(value))}>```.
    """

    MAX_CHARS_PER_VALUE: int = Field(default=2000, exclude=True)

    def __rich_repr__(self):

        for name, field_info in self.__class__.model_fields.items():

            if field_info.exclude:
                continue

            value = getattr(self, name)
            length = len(str(value))

            if length > self.MAX_CHARS_PER_VALUE:
                yield name, f"<object of length: {len(str(value))}>"
            else:
                yield name, value