Skip to content

Instantly share code, notes, and snippets.

@brianherman
Created March 12, 2016 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianherman/457379b989aa6f863a68 to your computer and use it in GitHub Desktop.
Save brianherman/457379b989aa6f863a68 to your computer and use it in GitHub Desktop.
import sys
import cPickle
from PyQt4.QtCore import *
from PyQt4.QtGui import *
data = {'root': {'persons': {'person': [{'name': {'last': 'bar', 'first': 'foo'}}, {'name': {'last': 'bar', 'first': 'baz'}}]}}}
def fill_item(item, value):
item.setExpanded(True)
if type(value) is dict:
for (key, val) in sorted(value.iteritems()):
child = QTreeWidgetItem()
child.setText(0, unicode(key))
item.addChild(child)
fill_item(child, val)
elif type(value) is list:
for val in value:
child = QTreeWidgetItem()
item.addChild(child)
if type(val) is dict:
child.setText(0, '[dict]')
fill_item(child, val)
elif type(val) is list:
child.setText(0, '[list]')
fill_item(child, val)
else:
child.setText(0, unicode(val))
child.setExpanded(True)
else:
child = QTreeWidgetItem()
child.setText(0, unicode(value))
item.addChild(child)
def fill_widget(widget, value):
widget.clear()
fill_item(widget.invisibleRootItem(), value)
def main():
dictionary = {}
if len(sys.argv) > 2:
with open(sys.argv[1]) as dict_file:
dictionary = cPickle.load(dict_file.read())
app = QApplication(sys.argv)
widget = QTreeWidget()
fill_widget(widget, dictionary)
widget.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment