Skip to content

Instantly share code, notes, and snippets.

@luser
Created August 17, 2016 17:57
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 luser/a9bab41bd9102cb4055429c5c79a2a11 to your computer and use it in GitHub Desktop.
Save luser/a9bab41bd9102cb4055429c5c79a2a11 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from __future__ import unicode_literals, print_function
import codecs
import operator
import sys
in_data = {'a':1, 'b': u'abc', 'c': 'xyz', 'd': False, 'e': {'a': 1, 'b': u'2', 'c': '3'}, 'f': [1, u'2', '3'],
'some_unicode': u'\U0001f4a9',
'some_bytes': b'a\xffb',
}
utype = type(u'')
str_is_bytes = str is bytes
def simple_repr(o, f, indent=0):
if isinstance(o, dict):
f.write('{\n')
for k, v in sorted(o.items(), key=operator.itemgetter(0)):
f.write(' ' * (indent + 2))
simple_repr(k, f, indent + 2)
f.write(': ')
simple_repr(v, f, indent + 2)
f.write(',\n')
f.write(' ' * indent)
f.write('}')
elif isinstance(o, bytes):
if str_is_bytes:
f.write('b')
f.write(repr(o))
elif isinstance(o, utype):
f.write("'")
f.write(o)
f.write("'")
elif hasattr(o, '__iter__'):
f.write('[\n')
for i in o:
f.write(' ' * (indent + 2))
simple_repr(i, f, indent + 2)
f.write(',\n')
f.write(' ' * indent)
f.write(']')
else:
f.write(repr(o))
f = codecs.open('/dev/null', 'w', 'utf-8')
if False:
#XXX: comment out the 'some_bytes' line in in_data or json.dump will choke
import timeit
print('json.dump(): %f' % timeit.timeit(stmt='json.dump(in_data, f, sort_keys=True, ensure_ascii=False, indent=2)', number=100000, setup="import json; from __main__ import in_data, f"))
print('simple_repr(): %f' % timeit.timeit(stmt='simple_repr(in_data, f)', number=100000, setup="from __main__ import simple_repr, in_data, f"))
if True:
out = codecs.open(sys.argv[1], 'w', 'utf-8') if len(sys.argv) > 1 else sys.stdout
out.write('''# coding: utf-8
from __future__ import unicode_literals
out_data = ''')
simple_repr(in_data, out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment