Skip to content

Commit b301986

Browse files
daiyippyglove authors
authored and
pyglove authors
committed
Support markdown quotes on the repr/str of Formattable objects.
This allows users to use `pg.str_format` and `pg.repr_format` context manager to add markdown quotes to the repr/str of symbolic objects, which serves the scenario when embedding objects within natural language string. PiperOrigin-RevId: 595596634
1 parent 9cd070d commit b301986

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

pyglove/core/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@
279279
MISSING_VALUE = object_utils.MISSING_VALUE
280280

281281
Formattable = object_utils.Formattable
282+
repr_format = object_utils.repr_format
283+
str_format = object_utils.str_format
284+
282285
MaybePartial = object_utils.MaybePartial
283286
JSONConvertible = object_utils.JSONConvertible
284287
DocStr = object_utils.DocStr

pyglove/core/object_utils/common_traits.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,31 @@ def __str__(self) -> str:
7676
"""Returns the full (maybe multi-line) representation of this object."""
7777
kwargs = dict(self.__str_format_kwargs__)
7878
kwargs.update(thread_local.thread_local_kwargs(_TLS_STR_FORMAT_KWARGS))
79-
return self.format(**kwargs)
79+
return self._maybe_quote(self.format(**kwargs), **kwargs)
8080

8181
def __repr__(self) -> str:
8282
"""Returns a single-line representation of this object."""
8383
kwargs = dict(self.__repr_format_kwargs__)
8484
kwargs.update(thread_local.thread_local_kwargs(_TLS_REPR_FORMAT_KWARGS))
85-
return self.format(**kwargs)
85+
return self._maybe_quote(self.format(**kwargs), **kwargs)
86+
87+
def _maybe_quote(
88+
self,
89+
s: str,
90+
*,
91+
compact: bool = False,
92+
root_indent: int = 0,
93+
markdown: bool = False,
94+
**kwargs
95+
) -> str:
96+
"""Maybe quote the formatted string with markdown."""
97+
del kwargs
98+
if not markdown or root_indent > 0:
99+
return s
100+
if compact:
101+
return f'`{s}`'
102+
else:
103+
return f'\n```\n{s}\n```\n'
86104

87105

88106
class MaybePartial(metaclass=abc.ABCMeta):

pyglove/core/object_utils/common_traits_test.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,24 @@
1919

2020
class Foo(common_traits.Formattable):
2121

22-
def format(self, compact: bool = False, verbose: bool = True, **kwargs):
22+
def format(
23+
self, compact: bool = False, verbose: bool = True, **kwargs):
2324
return f'{self.__class__.__name__}(compact={compact}, verbose={verbose})'
2425

2526

27+
class Bar(common_traits.Formattable):
28+
29+
def __init__(self, foo: Foo):
30+
self._foo = foo
31+
32+
def format(
33+
self, compact: bool = False, verbose: bool = True,
34+
root_indent: int = 0, **kwargs):
35+
foo_str = self._foo.format(
36+
compact=compact, verbose=verbose, root_indent=root_indent + 1)
37+
return f'{self.__class__.__name__}(foo={foo_str})'
38+
39+
2640
class FormattableTest(unittest.TestCase):
2741

2842
def test_formattable(self):
@@ -31,13 +45,13 @@ def test_formattable(self):
3145
self.assertEqual(str(foo), 'Foo(compact=False, verbose=True)')
3246

3347
def test_formattable_with_custom_format(self):
34-
class Bar(Foo):
48+
class Baz(Foo):
3549
__str_format_kwargs__ = {'compact': False, 'verbose': False}
3650
__repr_format_kwargs__ = {'compact': True, 'verbose': False}
3751

38-
bar = Bar()
39-
self.assertEqual(repr(bar), 'Bar(compact=True, verbose=False)')
40-
self.assertEqual(str(bar), 'Bar(compact=False, verbose=False)')
52+
bar = Baz()
53+
self.assertEqual(repr(bar), 'Baz(compact=True, verbose=False)')
54+
self.assertEqual(str(bar), 'Baz(compact=False, verbose=False)')
4155

4256
def test_formattable_with_context_managers(self):
4357
foo = Foo()
@@ -46,6 +60,13 @@ def test_formattable_with_context_managers(self):
4660
self.assertEqual(repr(foo), 'Foo(compact=False, verbose=True)')
4761
self.assertEqual(str(foo), 'Foo(compact=False, verbose=False)')
4862

63+
bar = Bar(foo)
64+
with common_traits.repr_format(markdown=True):
65+
self.assertEqual(repr(bar), '`Bar(foo=Foo(compact=True, verbose=True))`')
66+
with common_traits.str_format(markdown=True):
67+
self.assertEqual(
68+
str(bar), '\n```\nBar(foo=Foo(compact=False, verbose=True))\n```\n')
69+
4970

5071
class ExplicitlyOverrideTest(unittest.TestCase):
5172

0 commit comments

Comments
 (0)