2024-05-04 22:09:32 +02:00
|
|
|
import pprint
|
2024-02-07 17:28:16 +01:00
|
|
|
from collections.abc import Sequence
|
|
|
|
from dataclasses import asdict, dataclass
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class DataclassPPrintMixin:
|
2024-05-04 22:09:32 +02:00
|
|
|
def pprint_asdict(self, exclude_fields: Sequence[str] | None = None, indent: int = 4) -> None:
|
2024-02-07 17:28:16 +01:00
|
|
|
"""Pretty-print the object as a dict, excluding specified fields.
|
|
|
|
|
|
|
|
:param exclude_fields: A sequence of field names to exclude from the output.
|
|
|
|
If None, no fields are excluded.
|
2024-05-04 22:09:32 +02:00
|
|
|
:param indent: The indentation to use when pretty-printing.
|
2024-02-07 17:28:16 +01:00
|
|
|
"""
|
2024-05-04 22:09:32 +02:00
|
|
|
print(self.pprints_asdict(exclude_fields=exclude_fields, indent=indent))
|
|
|
|
|
|
|
|
def pprints_asdict(self, exclude_fields: Sequence[str] | None = None, indent: int = 4) -> str:
|
|
|
|
"""String corresponding to pretty-print of the object as a dict, excluding specified fields.
|
|
|
|
|
|
|
|
:param exclude_fields: A sequence of field names to exclude from the output.
|
|
|
|
If None, no fields are excluded.
|
|
|
|
:param indent: The indentation to use when pretty-printing.
|
|
|
|
"""
|
|
|
|
prefix = f"{self.__class__.__name__}\n----------------------------------------\n"
|
2024-02-07 17:28:16 +01:00
|
|
|
print_dict = asdict(self)
|
|
|
|
exclude_fields = exclude_fields or []
|
|
|
|
for field in exclude_fields:
|
|
|
|
print_dict.pop(field, None)
|
2024-05-04 22:09:32 +02:00
|
|
|
return prefix + pprint.pformat(print_dict, indent=indent)
|