Skip to content

API Reference

trivrepr.TrivialRepr

Helper class that generates a repr() string.

Derived classes should take care that all arguments to the init() method correspond to object attributes with exactly the same names.

Source code in src/trivrepr/__init__.py
class TrivialRepr:
    """Helper class that generates a repr() string.

    Derived classes should take care that all arguments to the __init__()
    method correspond to object attributes with exactly the same names.
    """

    def _trivrepr_attrlist(self) -> list[str]:
        """Return the names of the attributes to display."""
        spec: Final = inspect.getfullargspec(self.__init__)  # type: ignore[misc]
        return [*spec.args[1:], *spec.kwonlyargs]

    def __repr__(self) -> str:
        """Provide a Python-esque representation of the object."""
        attrs: Final = ", ".join(
            f"{name}={getattr(self, name)!r}" for name in self._trivrepr_attrlist()
        )
        return f"{type(self).__name__}({attrs})"

__repr__()

Provide a Python-esque representation of the object.

Source code in src/trivrepr/__init__.py
def __repr__(self) -> str:
    """Provide a Python-esque representation of the object."""
    attrs: Final = ", ".join(
        f"{name}={getattr(self, name)!r}" for name in self._trivrepr_attrlist()
    )
    return f"{type(self).__name__}({attrs})"

trivrepr.TrivialReprWithJson

Bases: TrivialRepr

Helper class that also provides a .to_json() method.

Source code in src/trivrepr/__init__.py
class TrivialReprWithJson(TrivialRepr):
    """Helper class that also provides a `.to_json()` method."""

    def to_json(self) -> dict[str, Any]:
        """Provide a dictionary describing the object's properties.

        Any property value that is an object implementing a `.to_json()`
        method will have this method invoked to obtain the dictionary value.
        Dictionaries, lists, and sets will be recursively descended into
        (set elements are also sorted); `pathlib.Path` objects will be
        converted into strings. Any other objects are stored as they are.
        """
        return {name: _to_json(getattr(self, name)) for name in self._trivrepr_attrlist()}

to_json()

Provide a dictionary describing the object's properties.

Any property value that is an object implementing a .to_json() method will have this method invoked to obtain the dictionary value. Dictionaries, lists, and sets will be recursively descended into (set elements are also sorted); pathlib.Path objects will be converted into strings. Any other objects are stored as they are.

Source code in src/trivrepr/__init__.py
def to_json(self) -> dict[str, Any]:
    """Provide a dictionary describing the object's properties.

    Any property value that is an object implementing a `.to_json()`
    method will have this method invoked to obtain the dictionary value.
    Dictionaries, lists, and sets will be recursively descended into
    (set elements are also sorted); `pathlib.Path` objects will be
    converted into strings. Any other objects are stored as they are.
    """
    return {name: _to_json(getattr(self, name)) for name in self._trivrepr_attrlist()}