Formats a dictionary's values as a nice string that's easier to read and ready to display. Uses pprint to format the dictionary :param dict_var: The dictionary to display :type dict_var: (dict) :param sort_keys: If True then the keys are sorted alphabetically :type sor
(dict_var, sort_keys=True, compact=False, indent=10, width=None)
| 20252 | |
| 20253 | |
| 20254 | def dict_to_string(dict_var, sort_keys=True, compact=False, indent=10, width=None): |
| 20255 | """ |
| 20256 | Formats a dictionary's values as a nice string that's easier to read and ready to display. |
| 20257 | Uses pprint to format the dictionary |
| 20258 | :param dict_var: The dictionary to display |
| 20259 | :type dict_var: (dict) |
| 20260 | :param sort_keys: If True then the keys are sorted alphabetically |
| 20261 | :type sort_keys: (bool) |
| 20262 | :param compact: If True then pprint's compact mode is used |
| 20263 | :type compact: (bool) |
| 20264 | :param indent: Number of spaces to indent (passed to pprint directly) |
| 20265 | :type indent: (int) |
| 20266 | :param width: specifies the desired maximum number of characters per line in the output. Defaults to 1 (sounds weird but it works best). Settablein PySimpleGUI Settings window |
| 20267 | :type width: (int) |
| 20268 | :return: Formatted output of the dictionary's values |
| 20269 | :rtype: (str) |
| 20270 | """ |
| 20271 | |
| 20272 | if width is None: |
| 20273 | width = pysimplegui_user_settings.get('-pformat width2-', DEFAULT_DICT_TO_STRING_WIDTH) |
| 20274 | |
| 20275 | if sys.version_info >= (3, 8): |
| 20276 | string = pprint.pformat(dict_var, sort_dicts=sort_keys, compact=compact, indent=indent, width=width) |
| 20277 | else: # sort_dicts wasn't added until Python 3.8 |
| 20278 | string = pprint.pformat(dict_var, compact=compact, indent=indent, width=width) |
| 20279 | return string |
| 20280 | |
| 20281 | |
| 20282 |