in google/generativeai/string_utils.py [0:0]
def _prettyprint(self):
"""A dataclass prettyprint function you can use in __str__or __repr__.
Note: You can't set `__str__ = pprint.pformat` because it causes a recursion error.
Mostly identical to pprint but:
* This will contract long lists and dicts (> 10lines) to [...] and {...}.
* This will contract long object reprs to ClassName(...).
"""
fields = []
for f in dataclasses.fields(self):
s = pprint.pformat(getattr(self, f.name))
class_re = r"^(\w+)\(.*\)$"
if s.count("\n") >= 10:
if s.startswith("["):
s = "[...]"
elif s.startswith("{"):
s = "{...}"
elif re.match(class_re, s, flags=re.DOTALL):
s = re.sub(class_re, r"\1(...)", s, flags=re.DOTALL)
else:
s = "..."
else:
width = len(f.name) + 1
s = textwrap.indent(s, " " * width).lstrip(" ")
fields.append(f"{f.name}={s}")
attrs = ",\n".join(fields)
name = self.__class__.__name__
width = len(name) + 1
attrs = textwrap.indent(attrs, " " * width).lstrip(" ")
return f"{name}({attrs})"