On Tue, Jun 22, 2010 at 08:24:28AM -0500, Michael Urman wrote: > On Tue, Jun 22, 2010 at 00:28, Stephen J. Turnbull <step...@xemacs.org> wrote: > > Michael Urman writes: > > > > > It is somewhat troublesome that there doesn't appear to be an obvious > > > built-in idempotent-when-possible function that gives back the > > > provided bytes/str, > > > > If you want something idempotent, it's already the case that > > bytes(b'abc') => b'abc'. What might be desirable is to make > > bytes('abc') work and return b'abc', but only if 'abc' is pure ASCII > > (or maybe ISO 8859/1). > > By idempotent-when-possible, I mean to_bytes(str_or_bytes, encoding, > errors) that would pass an instance of bytes through, or encode an > instance of str. And of course a to_str that performs similarly, > passing str through and decoding bytes. While bytes(b'abc') will give > me b'abc', neither bytes('abc') nor bytes(b'abc', 'latin-1') get me > the b'abc' I want to see. > A month or so ago, I finally broke down and wrote a python2 library that had these functions in it (along with a bunch of other trivial boilerplate functions that I found myself writing over and over in different projects)
https://fedorahosted.org/releases/k/i/kitchen/docs/api-text-converters.html#unicode-and-byte-str-conversion I suppose I could port this to python3 and we could see if it gains adoption as a thirdparty addon. I have been hesitating over doing that since I don't use python3 for everyday work and I have a vague feeling that 2to3 won't understand what that code needs to do. -Toshio
pgpi8QfNv3gC0.pgp
Description: PGP signature
_______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com