Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| python [2016/10/10 16:41] – gevatter | python [2016/10/10 16:42] (current) – gevatter | ||
|---|---|---|---|
| Line 17: | Line 17: | ||
| </ | </ | ||
| source: http:// | source: http:// | ||
| + | |||
| + | ===== json debugging, colored output ===== | ||
| + | < | ||
| + | # | ||
| + | |||
| + | import json | ||
| + | import requests | ||
| + | from pprint import pprint | ||
| + | from pygments import highlight | ||
| + | from pygments.lexers import PythonLexer | ||
| + | from pygments.formatters import Terminal256Formatter | ||
| + | from pprint import pformat | ||
| + | |||
| + | def pprint_color(obj): | ||
| + | print highlight(pformat(obj), | ||
| + | |||
| + | data = { | ||
| + | u' | ||
| + | u' | ||
| + | |||
| + | url = ' | ||
| + | |||
| + | r = requests.post( | ||
| + | url, | ||
| + | data=json.dumps(data)) | ||
| + | |||
| + | try: | ||
| + | pprint_color( | ||
| + | json.loads(r.text)) | ||
| + | except ValueError: | ||
| + | print r.text | ||
| + | </ | ||
