yEnc implementation in Python, bit slow
Freddie
oinkfreddie at oinkshlick.oinknet
Tue Aug 5 00:23:57 EDT 2003
Oren Tirosh <oren-py-l at hishome.net> wrote in
news:mailman.1060033689.18067.python-list at python.org:
> Suggestions:
>
> For the (x+42)%256 build a translation table and use str.translate.
> To encode characters as escape sequences use str.replace or re.sub.
>
> Oren
Aahh. I couldn't work out how to use translate() at 4am this morning, but I
worked it out now :) This version is a whoooole lot faster, and actually
meets the yEnc line splitting spec. Bonus!
$ python2.3 testyenc.py
yEncode1 407682 1.98
yEncode2 407707 0.18
I'm not sure how to use re.sub to escape the characters, I assume it would
also be 4 seperate replaces? Also, it needs a slightly more random input
string than 'a' * 400000, so here we go.
test = []
for i in xrange(256):
test.append(chr(i))
teststr = ''.join(test*1562)
def yEncode2(data):
trans = ''
for i in range(256):
trans += chr((i+42)%256)
translated = data.translate(trans)
# escape =, NUL, LF, CR
for i in (61, 0, 10, 13):
j = '=%c' % (i + 64)
translated = translated.replace(chr(i), j)
encoded = []
n = 0
for i in range(0, len(translated), 256):
chunk = translated[n+i:n+i+256]
if chunk[-1] == '=':
chunk += translated[n+i+256+1]
n += 1
encoded.append(chunk)
encoded.append('\n')
result = ''.join(encoded)
print len(result),
return result
--
-----------------------------------------------------------
Remove the oinks!
More information about the Python-list
mailing list