FAQ
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!

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 3 of 5 | next ›
Discussion Overview
grouppython-list @
categoriespython
postedAug 4, '03 at 2:50p
activeAug 6, '03 at 8:34a
posts5
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase