MappingReprMixin

Contents

MappingReprMixin#

class brainstate.util.MappingReprMixin[source]#

Mapping mixin for pretty representation.

This mixin only supplies a __pretty_repr__ implementation (the hook consumed by pretty_repr_object()); it does not override __repr__ or __str__. In particular, mixing it into dict does not change how the object prints, because dict.__repr__ still wins via the MRO. To obtain the multi-line pretty string you must route through pretty_repr_object(), which also requires the object to be a PrettyRepr instance.

Examples

>>> from brainstate.util._pretty_repr import (
...     MappingReprMixin, PrettyRepr, pretty_repr_object
... )
>>>
>>> # Combine with ``PrettyRepr`` and expose the pretty output through an
>>> # explicit ``__repr__`` (``dict.__repr__`` would otherwise shadow it).
>>> class PrettyDict(dict, MappingReprMixin, PrettyRepr):
...     def __repr__(self):
...         return pretty_repr_object(self)
...
>>> print(PrettyDict({'a': 1, 'b': 2}))
{
  'a': 1,
  'b': 2
}