1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
import io
import sys
from rich.pretty import pprint
def improve_data_docstring(data, lines):
"""
Improve the documentation of data by pretty-printing into in the docstring.
:param data: The documented object
:type data: object
:param lines: The lines of docstring lines
:type lines: list [ str ]
"""
if isinstance(data, (list, tuple, dict, set)):
# Redirect stdout to StringIO to catch print
old_stdout = sys.stdout
new_stdout = io.StringIO()
sys.stdout = new_stdout
# Pretty print iterable
pprint(data)
output = new_stdout.getvalue()
# Append pretty printed lines
lines.append(".. code-block:: JavaScript")
lines.append("")
lines.append(" " + output)
# Reset stdout
sys.stdout = old_stdout
|