string repr in 2.1

Robin Becker robin at jessikat.fsnet.co.uk
Tue May 29 09:22:08 EDT 2001


In article <6qbsocfhmy.fsf at abnoba.intevation.de>, Bernhard Herzog
<bh at intevation.de> writes
>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
>
well mostly I hacked this together in about 5 minutes; generating the
table isn't the main problem. 

Using the oct func is fine and I guess I should check exactly which
chars PDF needs. Also it's smart to truncate the excess leading zeros.

However, your mapping version is still twice as long as the original 2.0
version :( I see 1.7-1.8 seconds vs 3.9-4.0 seconds. Certainly better
than the re version. Thanks.
-- 
Robin Becker



More information about the Python-list mailing list