Skip to content

Instantly share code, notes, and snippets.

@samclane
Last active February 7, 2016 18:42
Show Gist options
  • Save samclane/c2e1bee32cd018407f04 to your computer and use it in GitHub Desktop.
Save samclane/c2e1bee32cd018407f04 to your computer and use it in GitHub Desktop.
Pretty print a nested dictionary into a RichTextCtrl(self)
def pretty_print(self, elem, level=0):
"""
Adds indentation to output of an ElementTree based on depth of element. Recursive.
"""
# Todo: Write to be compliant with a dictionary
i = "\r\n" + 2 * level * " "
for item in elem:
(attr, val) = item
if not type(val) is dict:
self.BeginBold()
self.WriteText(i + attr + ": ")
self.EndBold()
self.WriteText(val)
else:
self.BeginBold()
self.WriteText(i + attr + ": ")
self.EndBold()
self.pretty_print(val.items(), level + 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment