string repr in 2.1
Bernhard Herzog
bh at intevation.de
Tue May 29 08:45:41 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. Python wouldn't be backwards compatible otherwise.
> 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?
>
> My test code looks like
>
> ######################################
> import sys, string
> from time import time
> _ESCAPELIST=256*[')']
> for c, n in {'\\':'\\\\', '(':'\\(',')':'\\)'}.items():
> _ESCAPELIST[ord(c)]=n
> for c in range(0,32)+range(127,256):
> n = '\\'
> for i in (6,3,0):
> n += "01234567"[(c>>i)&7]
> _ESCAPELIST[c] = n
Why not simply
_ESCAPELIST[c] = '\\%03o' % c
?
Or for that matter, you could borrow some code from Sketch (taken from
Sketch/Lib/psmisc.py):
from string import join
import operator
def make_ps_quote_table():
table = [''] * 256
quote = (ord('('), ord(')'), ord('\\'))
for i in range(128):
if i in quote:
table[i] = '\\' + chr(i)
else:
table[i] = chr(i)
for i in range(128, 256):
table[i] = '\\' + oct(i)[1:]
return table
quote_table = make_ps_quote_table()
def quote_ps_string(text):
return join(map(operator.getitem, [quote_table]*len(text), map(ord, text)),
'')
Bernhard
--
Intevation GmbH http://intevation.de/
Sketch http://sketch.sourceforge.net/
MapIt! http://mapit.de/
More information about the Python-list
mailing list