class_docstr: Creates string representations of objects useful for logging / snapshots.
class_docstr_tok: ["Creates","string","representations","of","objects","useful","for","logging","\/","snapshots","."]
docstr: Convert a key / value for a mapping-like object to a string that should be placed in an indented block.
  
  Mapping-like objects include dictionaries, dataclasses, and Pydantic models. The output of this method would
  look like:
  
  "'key': [1, 2, 3]" or "arg=Foo()", etc.
  
  and the caller of this method would add the actual indent.
  
  Args:
      key: The object representing the key.
      value: The object representing the value.
      key_value_seperator: The string used to separate the key and the value. e.g. ": " for dicts, "=" for
      dataclasses.
      is_dataclass_like_object: Set this to True if the given value object is a dataclass to handle some printing
      options specific to dataclasses.
      remaining_line_width: If specified, try to make the string representation <= this many columns wide.
  
  Returns:
      The block that represents the key / value item and goes in between "[" / "]" in the string representation
      of the mapping-like object.
docstr_tok: ["Convert","a","key","\/","value","for","a","mapping-like","object","to","a","string","that","should","be","placed","in","an","indented","block",".","Mapping-like","objects","include","dictionaries",",","dataclasses",",","and","Pydantic","models",".","The","output","of","this","method","would","look","like",":","''","'key","'",":","[","1",",","2",",","3","]","''","or","``","arg=Foo","(",")","''",",","etc",".","and","the","caller","of","this","method","would","add","the","actual","indent",".","Args",":","key",":","The","object","representing","the","key",".","value",":","The","object","representing","the","value",".","key_value_seperator",":","The","string","used","to","separate","the","key","and","the","value",".","e.g",".","``",":","``","for","dicts",",","``","=","''","for","dataclasses",".","is_dataclass_like_object",":","Set","this","to","True","if","the","given","value","object","is","a","dataclass","to","handle","some","printing","options","specific","to","dataclasses",".","remaining_line_width",":","If","specified",",","try","to","make","the","string","representation","<","=","this","many","columns","wide",".","Returns",":","The","block","that","represents","the","key","\/","value","item","and","goes","in","between","``","[","``","\/","``","]","''","in","the","string","representation","of","the","mapping-like","object","."]
code: def _handle_indented_key_value_item(  # type: ignore[misc]
      self,
      key: Any,
      value: Any,
      key_value_seperator: str,
      is_dataclass_like_object: bool,
      remaining_line_width: Optional[int],
  ) -> str:
      """Convert a key / value for a mapping-like object to a string that should be placed in an indented block.
  
      Mapping-like objects include dictionaries, dataclasses, and Pydantic models. The output of this method would
      look like:
  
      "'key': [1, 2, 3]" or "arg=Foo()", etc.
  
      and the caller of this method would add the actual indent.
  
      Args:
          key: The object representing the key.
          value: The object representing the value.
          key_value_seperator: The string used to separate the key and the value. e.g. ": " for dicts, "=" for
          dataclasses.
          is_dataclass_like_object: Set this to True if the given value object is a dataclass to handle some printing
          options specific to dataclasses.
          remaining_line_width: If specified, try to make the string representation <= this many columns wide.
  
      Returns:
          The block that represents the key / value item and goes in between "[" / "]" in the string representation
          of the mapping-like object.
      """
      # See if the string representation can fit on one line. e.g. "'a': [1, 2]"
      if remaining_line_width is None or remaining_line_width > 0:
          result_items_without_limit: List[str] = []
          if (
              is_dataclass_like_object
              and self._include_object_field_names
          ):
              result_items_without_limit.append(str(key))
          else:
              result_items_without_limit.append(
                  self._handle_any_obj(key, remaining_line_width=None)
              )
          result_items_without_limit.append(key_value_seperator)
          result_items_without_limit.append(
              self._handle_any_obj(value, remaining_line_width=None)
          )
  
          result_without_limit = indent(
              "".join(result_items_without_limit),
              indent_prefix=self._indent_prefix,
          )
          if (
              remaining_line_width is None
              or len(result_without_limit) <= remaining_line_width
          ):
              return result_without_limit
  
      # The string representation can't fit on one line - use multiple. e.g.
      """
          'key':
            [1, 2, 3, 4]
          """
  
      # Create the string for the key.
      result_lines: List[str] = []
      if is_dataclass_like_object and self._include_object_field_names:
          result_lines.append(str(key) + key_value_seperator)
      else:
          # See if the key can be printed on one line. This depends on the length of the value as the key and the
          # the value as at least the first bit of the value is printed on the same line as the key.
          # e.g.
          # "foo"=[
          #   ...
          # ]
          min_length_of_first_value_line = len(
              self._handle_any_obj(
                  value, remaining_line_width=0
              ).splitlines()[0]
          )
  
          key_lines = self._handle_any_obj(
              key,
              remaining_line_width=remaining_line_width
              - len(key_value_seperator)
              - min_length_of_first_value_line,
          ).splitlines()
          # key_lines would be something like:
          # [
          #     "KeyObject(",
          #     "    a='foo',",
          #     "    b='bar',",
          #     ")",
          # ]
  
          if len(key_lines) == 1:
              result_lines.append(key_lines[0] + key_value_seperator)
          else:
              # The key needs to be printed in multiple lines. In that case, we want a result where the key value
              # separator is on the last line with the key. e.g.
              #
              # KeyObject(
              #     a='foo',
              #     b='bar',
              # ): ... <value>
              #
              result_lines.extend(key_lines[:-1])
              result_lines.append(key_lines[-1] + key_value_seperator)
  
      # Combine key and value.
  
      # Similar to the key, how we print the value depends on whether the value fits on one line or not. e.g.
      # foo=[1, 2, 3]
      #
      # or
      #
      # foo=[
      #   1,
      #   2,
      #   3,
      # ]
  
      # See if the value fits in the previous line.
      remaining_width_for_value = max(
          0, remaining_line_width - len(result_lines[-1])
      )
      value_str = self._handle_any_obj(
          value, remaining_line_width=remaining_width_for_value
      )
      value_lines = value_str.splitlines()
  
      if len(value_lines) <= 1:
          # Value can fit in the previous line
          result_lines[-1] = result_lines[-1] + value_lines[0]
      else:
          # For the multi-line value, we want to print the first line of the value on the same line as the last line
          # of the key.
          result_lines[-1] = result_lines[-1] + value_lines[0]
          result_lines.extend(value_lines[1:])
  
      return indent(
          "\n".join(result_lines), indent_prefix=self._indent_prefix
      )
  
file_path: metricflow/metricflow/mf_logging/pretty_print.py
imports: from __future__ import annotations
  import logging
  import pprint
  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 pydantic import BaseModel
  from metricflow.mf_logging.formatting import indent
label_desc: Use image node_id 4 for calling the MetricFlowPrettyFormatter obj's underlying member method code with example usage: obj._handle_indented_key_value_item(key, value, key_value_seperator, is_dataclass_like_object, remaining_line_width) and returns: indent, result_without_limit