Skip to content

Commit

Permalink
Streamline Object Formatting in Logs (#1418)
Browse files Browse the repository at this point in the history
This PR updates `mf_pformat_many()` to return a more compact string
representation.

* When possible, items are now formatted in a single line.
* Newlines between items were removed.
* Items are now formatted in an indented block.

Please see the updated tests to see what these changes look like.
  • Loading branch information
plypaul authored Sep 24, 2024
1 parent ca16c13 commit 9bee96f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections.abc import Mapping
from dataclasses import fields, is_dataclass
from enum import Enum
from typing import Any, Dict, List, Optional, Sized, Union
from typing import Any, Dict, List, Optional, Sized, Tuple, Union

from dsi_pydantic_shim import BaseModel

Expand Down Expand Up @@ -458,13 +458,18 @@ def mf_pformat_many( # type: ignore
include_empty_object_fields=include_empty_object_fields,
)

item_block_lines = (
f"{key}:",
indent(
value_str,
indent_prefix=indent_prefix,
),
)
lines_in_value_str = len(value_str.split("\n"))
item_block_lines: Tuple[str, ...]
if lines_in_value_str > 1:
item_block_lines = (
f"{key}:",
indent(
value_str,
indent_prefix=indent_prefix,
),
)
else:
item_block_lines = (f"{key}: {value_str}",)
item_block = "\n".join(item_block_lines)
lines.append(item_block)
return "\n\n".join(lines)
lines.append(indent(item_block))
return "\n".join(lines)
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,8 @@ def test_pformat_many() -> None: # noqa: D103
textwrap.dedent(
"""\
Example description:
object_0:
(1, 2, 3)
object_1:
{4: 5}
object_0: (1, 2, 3)
object_1: {4: 5}
"""
).rstrip()
== result
Expand All @@ -170,10 +166,9 @@ def test_pformat_many_with_raw_strings() -> None: # noqa: D103
textwrap.dedent(
"""\
Example description:
object_0:
foo
bar
object_0:
foo
bar
"""
).rstrip()
== result
Expand All @@ -186,9 +181,7 @@ def test_pformat_many_with_strings() -> None: # noqa: D103
textwrap.dedent(
"""\
Example description:
object_0:
'foo\\nbar'
object_0: 'foo\\nbar'
"""
).rstrip()
== result
Expand Down

0 comments on commit 9bee96f

Please sign in to comment.