Skip to content

Commit

Permalink
Add raw_strings argument to mf_pformat_many.
Browse files Browse the repository at this point in the history
  • Loading branch information
plypaul committed Mar 18, 2024
1 parent f46a0f7 commit 48ce567
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
29 changes: 20 additions & 9 deletions metricflow/mf_logging/pretty_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,21 +424,32 @@ def mf_pformat_many( # type: ignore
include_object_field_names: bool = True,
include_none_object_fields: bool = False,
include_empty_object_fields: bool = False,
raw_strings: bool = False,
) -> str:
"""Prints many objects in an indented form."""
"""Prints many objects in an indented form.
If raw_strings is set, and a value of the obj_dict is of type str, then use the value itself, not the
representation of the string. e.g. if value="foo", then "foo" instead of "'foo'". Useful for values that contain
newlines.
"""
lines: List[str] = [description]
for key, value in obj_dict.items():
if raw_strings and isinstance(value, str):
value_str = value
else:
value_str = mf_pformat(
obj=value,
max_line_length=max(0, max_line_length - len(indent_prefix)),
indent_prefix=indent_prefix,
include_object_field_names=include_object_field_names,
include_none_object_fields=include_none_object_fields,
include_empty_object_fields=include_empty_object_fields,
)

item_block_lines = (
f"{key}:",
indent(
mf_pformat(
obj=value,
max_line_length=max(0, max_line_length - len(indent_prefix)),
indent_prefix=indent_prefix,
include_object_field_names=include_object_field_names,
include_none_object_fields=include_none_object_fields,
include_empty_object_fields=include_empty_object_fields,
),
value_str,
indent_prefix=indent_prefix,
),
)
Expand Down
32 changes: 32 additions & 0 deletions metricflow/test/collection_helpers/test_pretty_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,35 @@ def test_pformat_many() -> None: # noqa: D
).rstrip()
== result
)


def test_pformat_many_with_raw_strings() -> None: # noqa: D
result = mf_pformat_many("Example description:", obj_dict={"object_0": "foo\nbar"}, raw_strings=True)

assert (
textwrap.dedent(
"""\
Example description:
object_0:
foo
bar
"""
).rstrip()
== result
)


def test_pformat_many_with_strings() -> None: # noqa: D
result = mf_pformat_many("Example description:", obj_dict={"object_0": "foo\nbar"})
assert (
textwrap.dedent(
"""\
Example description:
object_0:
'foo\\nbar'
"""
).rstrip()
== result
)

0 comments on commit 48ce567

Please sign in to comment.