# coding: utf-8 # import pprint import sys import locale from io import StringIO locale.setlocale(locale.LC_ALL, '') ascii_alnum = "R" non_ascii_alnum = "Ř" # used in a few Western Slavic languages unicode_alnum = "\u05d0" # Hebrew Aleph lst = (ascii_alnum, non_ascii_alnum, unicode_alnum) def test(args): locale.setlocale(locale.LC_ALL, args) print(locale.getlocale(locale.LC_ALL)) for char in lst: print("'{}'.isalnum(): {}".format(char, char.isalnum())) for char in lst: r = repr(char) s = pprint._safe_repr(char, {}, 0, 0)[0] print("'''{}''' == '''{}''': {}".format(r, s, r == s)) for args in ( '', 'C', ('en_US', 'ISO-8859-1'), ('zh_CN', '')): test(args) def old_str_repr(object): if 'locale' not in sys.modules: return repr(object), True, False if "'" in object and '"' not in object: closure = '"' quotes = {'"': '\\"'} else: closure = "'" quotes = {"'": "\\'"} qget = quotes.get sio = StringIO() write = sio.write for char in object: if char.isalpha(): write(char) else: write(qget(char, repr(char)[1:-1])) return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False def new_str_repr(object): return repr(object), True, False import timeit t1 = timeit.Timer("""old_str_repr({})""".format(repr(sys.copyright[:16])), "from __main__ import old_str_repr").timeit() t2 = timeit.Timer("""new_str_repr({})""".format(repr(sys.copyright[:16])), "from __main__ import new_str_repr").timeit() print("t1: {}, t2: {}, speedup: {} times".format( t1, t2, int(t1 // t2)))