Skip to content

Commit

Permalink
Support 'python_format' for pg.Symbolic.format method.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 524196407
  • Loading branch information
daiyip authored and pyglove authors committed Apr 14, 2023
1 parent e8b2520 commit c3005c6
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 6 deletions.
9 changes: 9 additions & 0 deletions pyglove/core/symbolic/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,17 @@ def format(self,
compact: bool = False,
verbose: bool = True,
root_indent: int = 0,
*,
python_format: bool = False,
hide_default_values: bool = False,
hide_missing_values: bool = False,
**kwargs) -> str:
"""Formats this object."""
kwargs.update({
'python_format': python_format,
'hide_default_values': hide_default_values,
'hide_missing_values': hide_missing_values,
})
details = object_utils.kvlist_str([
('parent_path', self.target.sym_path, None),
('path', self.path.path, None),
Expand Down
21 changes: 19 additions & 2 deletions pyglove/core/symbolic/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,11 +802,14 @@ def format(
compact: bool = False,
verbose: bool = True,
root_indent: int = 0,
*,
python_format: bool = False,
hide_default_values: bool = False,
hide_missing_values: bool = False,
exclude_keys: Optional[Set[str]] = None,
cls_name: Optional[str] = None,
bracket_type: object_utils.BracketType = object_utils.BracketType.CURLY,
key_as_attribute: bool = False,
**kwargs) -> str:
"""Formats this Dict."""
cls_name = cls_name or ''
Expand Down Expand Up @@ -848,8 +851,13 @@ def _indent(text, indent):
root_indent + 1,
hide_default_values=hide_default_values,
hide_missing_values=hide_missing_values,
python_format=python_format,
**kwargs)
kv_strs.append(f'{k}={v_str}')
if not python_format or key_as_attribute:
kv_strs.append(f'{k}={v_str}')
else:
kv_strs.append(f'\'{k}\': {v_str}')

s.append(', '.join(kv_strs))
s.append(close_bracket)
else:
Expand All @@ -871,8 +879,17 @@ def _indent(text, indent):
root_indent + 1,
hide_default_values=hide_default_values,
hide_missing_values=hide_missing_values,
python_format=python_format,
**kwargs)
s.append(_indent(f'{k} = {v_str}', root_indent + 1))
if not python_format:
# Format in PyGlove's format (default).
s.append(_indent(f'{k} = {v_str}', root_indent + 1))
elif key_as_attribute:
# Format `pg.Objects` under Python format.
s.append(_indent(f'{k}={v_str}', root_indent + 1))
else:
# Format regular `pg.Dict` under Python format.
s.append(_indent(f'\'{k}\': {v_str}', root_indent + 1))
s.append('\n')
s.append(_indent(close_bracket, root_indent))
return ''.join(s)
Expand Down
36 changes: 36 additions & 0 deletions pyglove/core/symbolic/dict_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,42 @@ def test_compact(self):
'A(x=2, y=MISSING_VALUE, z={p=[0: None, 1: True], '
'q=\'foo\', t=\'foo\'})}]}}}')

def test_compact_python_format(self):
self.assertEqual(
self._dict.format(compact=True, python_format=True),
'{\'a1\': 1, \'a2\': {\'b1\': {\'c1\': [{\'d1\': MISSING_VALUE, '
'\'d2\': True, \'d3\': A(x=2, y=MISSING_VALUE, z={\'p\': [None, True], '
'\'q\': \'foo\', \'t\': \'foo\'})}]}}}')

def test_noncompact_python_format(self):
self.assertEqual(
self._dict.format(compact=False, verbose=False, python_format=True),
inspect.cleandoc("""{
'a1': 1,
'a2': {
'b1': {
'c1': [
{
'd1': MISSING_VALUE(Str()),
'd2': True,
'd3': A(
x=2,
y=MISSING_VALUE(Str()),
z={
'p': [
None,
True
],
'q': 'foo',
't': 'foo'
}
)
}
]
}
}
}"""))

def test_noncompact_nonverbose(self):
self.assertEqual(
self._dict.format(compact=False, verbose=False),
Expand Down
18 changes: 14 additions & 4 deletions pyglove/core/symbolic/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,8 @@ def format(
compact: bool = False,
verbose: bool = True,
root_indent: int = 0,
*,
python_format: bool = False,
cls_name: Optional[str] = None,
bracket_type: object_utils.BracketType = object_utils.BracketType.SQUARE,
**kwargs) -> str:
Expand All @@ -719,8 +721,12 @@ def _indent(text, indent):
kv_strs = []
for idx, elem in enumerate(self):
v_str = object_utils.format(
elem, compact, verbose, root_indent + 1, **kwargs)
kv_strs.append(f'{idx}: {v_str}')
elem, compact, verbose, root_indent + 1,
python_format=python_format, **kwargs)
if python_format:
kv_strs.append(v_str)
else:
kv_strs.append(f'{idx}: {v_str}')
s.append(', '.join(kv_strs))
s.append(close_bracket)
else:
Expand All @@ -731,8 +737,12 @@ def _indent(text, indent):
else:
s.append(',\n')
v_str = object_utils.format(
elem, compact, verbose, root_indent + 1, **kwargs)
s.append(_indent(f'{idx} : {v_str}', root_indent + 1))
elem, compact, verbose, root_indent + 1,
python_format=python_format, **kwargs)
if python_format:
s.append(_indent(v_str, root_indent + 1))
else:
s.append(_indent(f'{idx} : {v_str}', root_indent + 1))
s.append('\n')
s.append(_indent(close_bracket, root_indent))
else:
Expand Down
38 changes: 38 additions & 0 deletions pyglove/core/symbolic/list_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,44 @@ def test_compact(self):
'A(x=2, y=MISSING_VALUE, z={p=[0: None, 1: True], '
'q=\'foo\', t=\'foo\'})}]}}}]')

def test_compact_python_format(self):
self.assertEqual(
self._list.format(compact=True, python_format=True),
'[{\'a1\': 1, \'a2\': {\'b1\': {\'c1\': [{\'d1\': MISSING_VALUE, '
'\'d2\': True, \'d3\': A(x=2, y=MISSING_VALUE, z={\'p\': [None, True], '
'\'q\': \'foo\', \'t\': \'foo\'})}]}}}]')

def test_noncompact_python_format(self):
self.assertEqual(
self._list.format(compact=False, verbose=False, python_format=True),
inspect.cleandoc("""[
{
'a1': 1,
'a2': {
'b1': {
'c1': [
{
'd1': MISSING_VALUE(Str()),
'd2': True,
'd3': A(
x=2,
y=MISSING_VALUE(Str()),
z={
'p': [
None,
True
],
'q': 'foo',
't': 'foo'
}
)
}
]
}
}
}
]"""))

def test_noncompact_nonverbose(self):
self.assertEqual(
self._list.format(compact=False, verbose=False),
Expand Down
1 change: 1 addition & 0 deletions pyglove/core/symbolic/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ def format(self,
verbose,
root_indent,
cls_name=self.__class__.__name__,
key_as_attribute=True,
bracket_type=object_utils.BracketType.ROUND,
**kwargs)

Expand Down
28 changes: 28 additions & 0 deletions pyglove/core/symbolic/object_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2595,6 +2595,34 @@ def test_compact(self):
'A(x=[0: A(x=1, y=None), 1: A(x=\'foo\', y={a=A(x=True, y=1.0)})], '
'y=MISSING_VALUE)')

def test_compact_python_format(self):
self.assertEqual(
self._a.format(compact=True, python_format=True),
'A(x=[A(x=1, y=None), A(x=\'foo\', y={\'a\': A(x=True, y=1.0)})], '
'y=MISSING_VALUE)')

def test_noncompact_python_format(self):
self.assertEqual(
self._a.format(compact=False, verbose=False, python_format=True),
inspect.cleandoc("""A(
x=[
A(
x=1,
y=None
),
A(
x='foo',
y={
'a': A(
x=True,
y=1.0
)
}
)
],
y=MISSING_VALUE(Any())
)"""))

def test_noncompact_nonverbose(self):
self.assertEqual(
self._a.format(compact=False, verbose=False),
Expand Down

0 comments on commit c3005c6

Please sign in to comment.