2to3 and maketrans

G

Gregory Ewing

What is the recommended way to write code for 2.7 using
maketrans() on text strings in such a way that it will
convert correctly using 2to3?

There seems to be two versions of maketrans in 3.x, one
for text and one for bytes. Code that references
string.maketrans ends up with the one for bytes, which
is not what I want. But I can't write str.maketrans,
because that doesn't exist in 2.x.

So what am I supposed to do?
 
M

Martin v. Loewis

Am 03.03.2011 07:58, schrieb Gregory Ewing:
What is the recommended way to write code for 2.7 using
maketrans() on text strings in such a way that it will
convert correctly using 2to3?

That depends on how you chose to represent text in 2.7.
The recommended way for that (also with 3.x in mind)
is that you should use Unicode strings to represent text.

Now, unicode strings support translation tables mapping
Unicode ordinals to Unicode strings in both 2.x and 3.x,
so I suggest you just don't use maketrans at all.

To give an example,

print u"hallo".translate({97:u'e'})

works fine in 2.x, and will work fine in 3.x when put
through 2to3 (which will convert the print and the unicode
literals).

If you chose to represent strings as bytes in 2.x, the
answer will be different.

Regards,
Martin
 
M

Martin v. Löwis

Am 04.03.2011 03:21, schrieb Dan Stromberg:
On Thu, Mar 3, 2011 at 3:46 PM, Martin v. Loewis <[email protected]

That depends on how you chose to represent text in 2.7.
The recommended way for that (also with 3.x in mind)
is that you should use Unicode strings to represent text.


For application programming, I'm sure Unicode is usually preferable.

For systems programming, it's hard for me to imagine that unicode would
normally be favored over bytes.

I think Greg Ewing was really talking about WxPython. For WxPython,
I would indeed recommend to represent text as Unicode, although
there may be backwards-compatibility concerns to support byte strings
in certain places as well.

Whether a GUI library is application programming or systems programming,
I don't know.

Regards,
Martin
 
G

Gregory Ewing

Martin said:
Whether a GUI library is application programming or systems programming,
I don't know.

Neither do I, but it doesn't really matter. In my case the
string is definitely text (although it will always be ascii)
and therefore unicode is the right representation to use
in 3.x.

I just wondered whether there was a recommended idiom for
using maketrans() on text in 2.7 that 2to3 would translate
into str.maketrans(), but it seems not. Instead the
solution seems to be to convert to unicode and use its
translation method instead.

I've since adopted a different solution, but I'll keep
this in mind for the future. Thanks, everyone.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top