utils#

openplaces utilities module

General-purpose utility functions for formatting, display, and debugging.

Functions#

pretty_print(→ str | None)

Pretty print nested dictionaries, lists, and Python class instances.

Module Contents#

openplaces.utils.pretty_print(obj: Any, indent: int = 2, max_depth: int = 10, return_string: bool = False, _current_depth: int = 0) str | None#

Pretty print nested dictionaries, lists, and Python class instances.

Converts dataclasses and objects with __dict__ to dictionaries recursively, then formats them in YAML-style (no quotes around keys, indentation-based).

By default, prints the output directly. Set return_string=True to get the string instead.

Parameters:
  • obj (Any) – Object to print (dict, list, dataclass, or any class instance)

  • indent (int, default=2) – Number of spaces per indentation level

  • max_depth (int, default=10) – Maximum nesting depth to prevent infinite recursion

  • return_string (bool, default=False) – If True, return the formatted string instead of printing it

  • _current_depth (int) – Internal parameter for tracking recursion depth

Returns:

If return_string=True, returns formatted string. Otherwise prints and returns None.

Return type:

str | None

Examples

>>> from dataclasses import dataclass
>>> @dataclass
... class Person:
...     name: str
...     age: int
>>>
>>> data = {'people': [Person('Alice', 30), Person('Bob', 25)]}
>>> pretty_print(data)
>>>
>>> # Get string without printing
>>> s = pretty_print(data, return_string=True)