Created
September 8, 2014 20:18
-
-
Save taldcroft/f61caa75a0718a1c8b8b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:2ad956e11f831e68c36ab4dfdff6dd1c1e0945200972c9bfe98254641a3c1bea" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import json\n", | |
"from collections import OrderedDict\n", | |
"import numpy as np" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class AnyEncoder(json.JSONEncoder):\n", | |
" def default(self, obj):\n", | |
" \"\"\"\n", | |
" This method gets called for any `obj` type that is unsupported by JSON.\n", | |
" In this case just delegate to the object __str__ method, which is\n", | |
" (basically) guaranteed to succeed.\n", | |
" \"\"\"\n", | |
" return str(obj)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class PrettyOrderedDict(OrderedDict):\n", | |
" \"\"\"\n", | |
" Use JSON to provide a pretty string representation for OrderedDict.\n", | |
" In this concept the astropy.utils.OrderedDict would be this subclass\n", | |
" and never the \"native\" collections.OrderedDict, even for python >= 2.7.\n", | |
" \"\"\"\n", | |
" def __str__(self):\n", | |
" return json.dumps(self, indent=4, separators=(',', ': '), cls=AnyEncoder)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class MyClass:\n", | |
" pass\n", | |
"mc = MyClass\n", | |
"\n", | |
"class MyList(list):\n", | |
" pass\n", | |
"ml = MyList\n", | |
"\n", | |
"class MyDict(dict):\n", | |
" pass\n", | |
"md = MyDict(xx=5)\n", | |
"\n", | |
"mod = PrettyOrderedDict([('bb', 2), ('aa', 1)])\n", | |
"\n", | |
"ar = np.arange(5)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Define a nested OrderedDict structure and see useless str() output**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"od = OrderedDict([('z', mc), \n", | |
" ('y', {'b': ml, \n", | |
" 'a': ar, \n", | |
" 'md': md, \n", | |
" 'mod': mod})])\n", | |
"print(od)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"OrderedDict([('z', <class __main__.MyClass at 0x104f7c598>), ('y', {'a': array([0, 1, 2, 3, 4]), 'md': {'xx': 5}, 'b': <class '__main__.MyList'>, 'mod': PrettyOrderedDict([('bb', 2), ('aa', 1)])})])\n" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Now the nicely formatted version utilizing the JSON encoder**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print(PrettyOrderedDict(od))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"{\n", | |
" \"z\": \"__main__.MyClass\",\n", | |
" \"y\": {\n", | |
" \"a\": \"[0 1 2 3 4]\",\n", | |
" \"md\": {\n", | |
" \"xx\": 5\n", | |
" },\n", | |
" \"b\": \"<class '__main__.MyList'>\",\n", | |
" \"mod\": {\n", | |
" \"bb\": 2,\n", | |
" \"aa\": 1\n", | |
" }\n", | |
" }\n", | |
"}\n" | |
] | |
} | |
], | |
"prompt_number": 6 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment