string repr in 2.1
Michael Hudson
mwh at python.net
Tue May 29 08:48:55 EDT 2001
Robin Becker <robin at jessikat.fsnet.co.uk> writes:
> On related notes if repr no longer does octal escapes does string
> input still accept them?
Yes! Upgrading isn't meant to be *that* much of an adventure.
> Also my trial of the 2.0 code versus an initial 2.1 coding reveals
> that an re based approach is very slow :( Anyone know simply what's
> involved in doing a codec?
Try this instead:
def _make_dict():
d = {}
for c in range(ord(' ')) + range(0x7F,0xFF):
d[chr(c)] = "\\%03o"%(c,)
for c in map(chr,range(ord(' '), 0x7F)):
d[c] = c
d['('] = '\\('
d[')'] = '\\)'
d['\\'] = '\\\\'
return d
_d = _make_dict()
def _escape21_mwh(self, s):
return ''.join(map(lambda c,d=_d: d[c], s))
It's faster than your re based version, but not as fast as _escape20;
I get:
$ /usr/local/src/Python-2.0/build-O2/python becker-doit.py 1000
Time for 1000x4 uses of <function _escape20 at 0x81ebb34> = 0.57"
Time for 1000x4 uses of <function _escape21 at 0x81e2174> = 2.30"
Time for 1000x4 uses of <function _escape21_mwh at 0x81e78f4> = 0.94"
It can be rewritten in C if you're really keen, when it'll probably
run somewhat faster than _escape20 (I have a very similar function in
pyrepl).
obligatory-jwz-quote-about-regexps-goes-here-ly y'rs
M.
--
Any form of evilness that can be detected without *too* much effort
is worth it... I have no idea what kind of evil we're looking for
here or how to detect is, so I can't answer yes or no.
-- Guido Van Rossum, python-dev
More information about the Python-list
mailing list