Source code for dnachisel.biotools.formatting_operations
"""Text and number formatting operations"""
from copy import deepcopy
import json
import numpy as np
[docs]def round_all_numbers_in_dict(d, rounding_digits=2, outplace=True):
""" Return a new version of dict d with all floats rounded to N digits."""
if outplace:
d = deepcopy(d)
for k, v in d.items():
if isinstance(v, float):
d[k] = np.round(v, rounding_digits)
if isinstance(v, dict):
round_all_numbers_in_dict(v, rounding_digits, outplace=False)
return d
[docs]def dict_to_pretty_string(d, rounding_digits=2, indent=2):
"""Return a nicely JSON-like formatted string to print a dict."""
d = round_all_numbers_in_dict(d, rounding_digits)
formatted_text = json.dumps(d, indent=indent)
for char in '{}",':
formatted_text = formatted_text.replace(char, "")
return formatted_text