3 ms·
"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
by eesmith 9y ago
"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)
- tyingq 9y agoSure...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.
- eesmith 9y agoAhh, yes, you're right. 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.