Re: Coding challenge: Optimise a custom string encoding

2014-08-19 Thread Lele Gaifax
Alex Willmer writes: > def plus_encode(s): > """Encode a unicode string with only ascii letters, digits, _, -, @, + > """ > bytemap_ = bytemap > s_utf8 = s.encode('utf-8') > return ''.join([bytemap[byte] for byte in s_utf8]) Minor nit: you defined a local alias for bytemap fo

Re: Coding challenge: Optimise a custom string encoding

2014-08-18 Thread Peter Otten
Alex Willmer wrote: > On Monday, 18 August 2014 21:16:26 UTC+1, Terry Reedy wrote: >> On 8/18/2014 3:16 PM, Alex Willmer wrote: >> > A challenge, just for fun. Can you speed up this function? >> >> You should give a specification here, with examples. You should perhaps > > Sorry, the (informal)

Re: Coding challenge: Optimise a custom string encoding

2014-08-18 Thread Chris Angelico
On Tue, Aug 19, 2014 at 5:16 AM, Alex Willmer wrote: > Back story: > Last week we needed a custom encoding to store unicode usernames in a config > file that only allowed mixed case ascii, digits, underscore, dash, at-sign > and plus sign. We also wanted to keeping the encoded usernames somewhat

Re: Coding challenge: Optimise a custom string encoding

2014-08-18 Thread Alex Willmer
On Monday, 18 August 2014 21:16:26 UTC+1, Terry Reedy wrote: > On 8/18/2014 3:16 PM, Alex Willmer wrote: > > A challenge, just for fun. Can you speed up this function? > > You should give a specification here, with examples. You should perhaps Sorry, the (informal) spec was further down. > > a

Re: Coding challenge: Optimise a custom string encoding

2014-08-18 Thread Terry Reedy
On 8/18/2014 3:16 PM, Alex Willmer wrote: A challenge, just for fun. Can you speed up this function? You should give a specification here, with examples. You should perhaps be using .maketrans and .translate. import string charset = set(string.ascii_letters + string.digits + '@_-') byteseq

Coding challenge: Optimise a custom string encoding

2014-08-18 Thread Alex Willmer
A challenge, just for fun. Can you speed up this function? import string charset = set(string.ascii_letters + string.digits + '@_-') byteseq = [chr(i) for i in xrange(256)] bytemap = {byte: byte if byte in charset else '+' + byte.encode('hex') for byte in byteseq} def plus_encode(s):