[Python-3000] PEP 3138- String representation in Python 3000
"Martin v. Löwis"
martin at v.loewis.de
Tue May 20 20:35:23 CEST 2008
> So you would like to force users to write e.g.
>
> def uu(input,errors='strict',filename='<data>',mode=0666):
> from cStringIO import StringIO
> from binascii import b2a_uu
> # using str() because of cStringIO's Unicode undesired Unicode
> behavior.
> infile = StringIO(str(input))
> outfile = StringIO()
> read = infile.read
> write = outfile.write
>
> # Encode
> write('begin %o %s\n' % (mode & 0777, filename))
> chunk = read(45)
> while chunk:
> write(b2a_uu(chunk))
> chunk = read(45)
> write(' \nend\n')
>
> return outfile.getvalue()
>
> (this is adapted Py2 code taken from the uu codec)
No. I would just use uu.encode instead, which already does the loop,
and everything else. So if I really wanted a string-to-string
conversion, I would do
infile = StringIO(input)
outfile = StringIO()
uu.encode(infile, outfile)
output = outfile.getvalue()
More likely, I have file-like objects already, in which case I won't
need to create StringIO objects.
Regards,
Martin
More information about the Python-3000
mailing list