Thursday, December 4, 2014

better translate()

translate is a simple, efficient function for doing fast byte-by-byte replacement on a string in Python.

However, Python Does What is always on the lookout for dangerous alternatives to simple things.  Instead of a function call, isn't it better to mutate global interpreter state so that the same result is achieved as a side-effect of a completely different operation?  (The answer is yes.)

>>> import ctypes
>>> def maketrans(inp, outp):
...    inp = list(inp)
...    outp = list(outp)
...    for aa, bb in zip(inp, outp):  # there's a fun reason single letter variable names are a bad idea here
...       ctypes.memset(ctypes.pythonapi.PyString_AsString(id(aa)), ord(bb), 1)
...
>>> maketrans("jkl", "xyz")
>>> "".join("jkl")
'xyz'

This method also has application to single character variable names that bears further exploration.

3 comments:

  1. Replies
    1. This method changes ALL single character strings in the interpreter. Including variable names.

      You can try it, leads to spectacular crashes.

      Delete
  2. These tire markdown codes will perseveringly not be found on any cashback, Looking for the latest Gousto voucher codes? Click here to get the latest 100% working markdown code for Gousto's astounding food boxes. Cooking gotten to a more focal level! Lakeland Furniture Discount Code of content grumblings.

    ReplyDelete