# Help to improve to this custom dict printing (or formatting) function?

**URL:** <https://discuss.python.org/t/help-to-improve-to-this-custom-dict-printing-or-formatting-function/74706>\
**Category:** Python Help\
**Tags:** help\
**Created:** [December 19, 2024, 2:31pm UTC](https://discuss.python.org/t/help-to-improve-to-this-custom-dict-printing-or-formatting-function/74706 "2024-12-19T14:31:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon81459992](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@anon81459992](https://discuss.python.org/u/anon81459992)\
**Post date:** [December 19, 2024, 2:31pm UTC](https://discuss.python.org/t/help-to-improve-to-this-custom-dict-printing-or-formatting-function/74706/1 "2024-12-19T14:31:43Z")

</div>

Been trying to write a fn that prints dictionaries as I like it. There surely are libraries but wanted to do it as an exercise.

I wonder what next-step improvements you’d suggest, or how would you re-write it, without going too far into expert-land?

(Maybe the first improvement is the name, but just leave that aside for now.)

The function is used within a logger class (that inherits from Logger), so it’s not logging there itself.

```python
"""Format dict for pretty printing."""

from collections.abc import Callable

def pp_dict(
  config: dict,
  max_depth: int = 5,
  *,
  _indent: int = 0,
) -> str:
  """Recursively clean up config classes and names for logging.

  Args:
    config: full configuration object or dictionary.
    max_depth: how far to go in nested dictionaries
    _indent: how many spaces to indent at sublevels (internal use only.)

  """
  to_join = []
  for k, v in config.items():
    new_val = v
    new_key = k
    if isinstance(v, Callable):
      new_val = v. __name__
    elif isinstance(v, dict):
      new_key = new_key.capitalize()
      to_join.append(f"\n{new_key}\n")
      if max_depth > 0:
        to_join.append(f"{pp_dict(v,max_depth=max_depth-1, _indent=_indent+2)}")
      else:
        msg = f"max_depth={max_depth} was reached due to deeply nested dicts."
        raise MaxDepthReachedError(msg)
      continue
    elif v is None:
      new_val = v. __class__. __name__ [:4]
    elif isinstance(v, bool | str | int | float | tuple):
      new_val = v
    to_join.append(f"{new_key.rjust(_indent)} = {new_val}")
  return "\n".join(to_join)

class MaxDepthReachedError(Exception):
  pass

```

---

<div class="post-metadata">

**Author:** ![anon81459992](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@anon81459992](https://discuss.python.org/u/anon81459992)\
**Post date:** [December 19, 2024, 2:40pm UTC](https://discuss.python.org/t/help-to-improve-to-this-custom-dict-printing-or-formatting-function/74706/2 "2024-12-19T14:40:45Z")

</div>

Just realised `max_depth`’s value doesn’t really make sense, it’d be 0 when erroring. Leaving it there though.

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [December 19, 2024, 2:49pm UTC](https://discuss.python.org/t/help-to-improve-to-this-custom-dict-printing-or-formatting-function/74706/3 "2024-12-19T14:49:49Z")

</div>

What does this need to do, that [pprint](https://docs.python.org/3/library/pprint.html) doesn’t already do?

---

<div class="post-metadata">

**Author:** ![anon81459992](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@anon81459992](https://discuss.python.org/u/anon81459992)\
**Post date:** [December 19, 2024, 2:55pm UTC](https://discuss.python.org/t/help-to-improve-to-this-custom-dict-printing-or-formatting-function/74706/4 "2024-12-19T14:55:39Z")

</div>

I know pprint, but as I said in the OP:

> There surely are libraries but wanted to do it as an exercise.

It does not handle class names by default, nor prints None as I prefer to see, nor capitalises the keys of nested dictionaries.

But aside from that, I’m just learning python so that’s why I find fun to re-invent (a lot worse) things.

---

<div class="post-metadata">

**Author:** ![AlSweigart](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/alsweigart/32/12237_2.png) [@AlSweigart](https://discuss.python.org/u/AlSweigart)\
**Post date:** [December 19, 2024, 4:58pm UTC](https://discuss.python.org/t/help-to-improve-to-this-custom-dict-printing-or-formatting-function/74706/5 "2024-12-19T16:58:36Z")

</div>

Keep in mind the possibility of loops in your data structure. Python and pprint already have ways of handling that:

```python
>>> x = {'a': None}
>>> x['a'] = x
>>> x
{'a': {...}}
>>> import pprint
>>> pprint.pprint(x)
{'a': <Recursion on dict with id=4308398400>}

```
