From 42fe0538931cd27de8f18b2426becf5432a1b8d3 Mon Sep 17 00:00:00 2001 From: Nelson Torres Date: Wed, 29 Jan 2025 23:48:54 +0000 Subject: [PATCH] Add `.log.mk_repr()` to create `reprlib.Repr`s --- piker/log.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/piker/log.py b/piker/log.py index 56776e1e..7f554f16 100644 --- a/piker/log.py +++ b/piker/log.py @@ -18,7 +18,11 @@ Log like a forester! """ import logging +import reprlib import json +from typing import ( + Callable, +) import tractor from pygments import ( @@ -84,3 +88,27 @@ def colorize_json( # likeable styles: algol_nu, tango, monokai formatters.TerminalTrueColorFormatter(style=style) ) + + +def mk_repr( + **repr_kws, +) -> Callable[[str], str]: + ''' + Allocate and deliver a `repr.Repr` instance with provided input + settings using the std-lib's `reprlib` mod, + * https://docs.python.org/3/library/reprlib.html + + ------ Ex. ------ + An up to 6-layer-nested `dict` as multi-line: + - https://stackoverflow.com/a/79102479 + - https://docs.python.org/3/library/reprlib.html#reprlib.Repr.maxlevel + + ''' + def_kws: dict[str, int] = dict( + indent=2, + maxlevel=6, # recursion levels + maxstring=66, # match editor line-len limit + ) + def_kws |= repr_kws + reprr = reprlib.Repr(**def_kws) + return reprr.repr