Earlier quoted context omitted.
What about it?
If you enter the code above into the website it responds with: maybe the code is wrong?
Automated Python 2 to 3 code translation
11–20 of 21 posts
Re: Automated Python 2 to 3 code translation
#12a = "test" if a
Re: Automated Python 2 to 3 code translation
#13Re: Automated Python 2 to 3 code translation
#14a = "test" if a
print(set(x for x in range(2),))Re: Automated Python 2 to 3 code translation
#15I don't mean to be negative, but how is this better than just using the `2to3` tool that already ships with Python?
But this web-2to3 is monetised with ads. The python community didn't think of that.
Re: Automated Python 2 to 3 code translation
#16I don't mean to be negative, but how is this better than just using the `2to3` tool that already ships with Python?
Re: Automated Python 2 to 3 code translation
#17a = "test" if a
There are some corner cases that run fine in python but trip up 2to3 tools, including this web based one. This code for example: print(set(x for x in range(2),))
Is there a tool which knows to translate this to:
import binascii
binascii.b2a_hex(b"asdf")
Even worse, if the string isn't known to be a byte string or ASCII unicode string, it's something like: import binascii
binascii.b2a_hex(s.encode("ascii") if isinstance(s, str) else s)Re: Automated Python 2 to 3 code translation
#18Re: Automated Python 2 to 3 code translation
#19Earlier quoted context omitted.
There are some corner cases that run fine in python but trip up 2to3 tools, including this web based one. This code for example: print(set(x for x in range(2),))
"asdf".encode("hex") Is there a tool which knows to translate this to: import binascii binascii.b2a_hex(b"asdf") Even worse, if the string isn't known to be a byte string or ASCII unicode string, it's something like: import binascii binascii.b2a_hex(s.encode("ascii") if isinstance(s, str) else s)
The snippet I posted is a little different in that it runs on both Python 2 as well as 3, but the 2to3 tools choke on it.
Re: Automated Python 2 to 3 code translation
#20Earlier quoted context omitted.
"asdf".encode("hex") Is there a tool which knows to translate this to: import binascii binascii.b2a_hex(b"asdf") Even worse, if the string isn't known to be a byte string or ASCII unicode string, it's something like: import binascii binascii.b2a_hex(s.encode("ascii") if isinstance(s, str) else s)
Sure...that's a different thing though. Python 3's choice to introduce new types and change behavior of existing types means no automated tool can really decide what to do. The snippet I posted is a little different in that it runs on both Python 2 as well as 3, but the 2to3 tools choke on it.
I'm still irritated by this specific case because my code and documentation used s.encode("hex") often, and it took a while to fix them all. Especially as the original code used both str and unicode hex-encoded values, so I couldn't drop in binascii. I ended up adding a C extension function.