classification
Title: PEP 378 uses replace where translate may work better
Type: behavior Stage:
Components: Documentation Versions: Python 3.1, Python 2.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, eric.smith, ncoghlan, r.david.murray, rhettinger, samwyse
Priority: normal Keywords:

Created on 2010-10-23 14:05 by samwyse, last changed 2010-10-23 21:55 by rhettinger. This issue is now closed.

Messages (4)
msg119427 - (view) Author: Samwyse (samwyse) Date: 2010-10-23 14:05
PEP 378 states;

  format(n, "6,f").replace(",", "X").replace(".", ",").replace("X", ".")

This is complex and relatively slow.  A better technique, which IMHO the proposal should high-lighted, would be:

  swap_commas_and_periods = bytes.maketrans(b',.', b'.,')
  format(n, "6,f").translate(swap_commas_and_periods)

While performing the maketrans each time a string is formatted is slower than the triple replace, calling it once and caching the result is faster.  I have tested with with the 3.1 interpreter; example timings follow.

>>> Timer("""
  '1,234,567.89'.replace(',', 'X').replace('.', ',').replace('X', '.')
""").timeit()
3.0645400462908015

>>> Timer("""
  '1,234,567.89'.translate(swap_commas_and_periods)
""", """
  swap_commas_and_periods = bytes.maketrans(b',.', b'.,')
""").timeit()
2.276630409730846


>>> Timer("""
  '1,234,567.89'.translate(bytes.maketrans(b',.', b'.,'))
""").timeit()
3.760715677551161
msg119429 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-10-23 14:30
The text in question is talking about 'replace' as a general mechanism for 'fixing' the separator character, and as such I don't think introducing translate would enhance the exposition.  I suppose it could be added in a footnote.
msg119445 - (view) Author: Samwyse (samwyse) Date: 2010-10-23 16:35
The text in question is also talking about the problems with using 'replace' to swap pairs of characters, so a better, alternate, process would be valuable, especially for anyone unaware of the translate method.
msg119477 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-10-23 21:55
Sorry, the text needs to stand as-is.
It is supposed to be a hint of what can be done,
nothing more.

The technique of t=x; x=y; y=t is somewhat basic
and has general applicability -- there's nothing
"complex" about it.   Also, for short strings 
such as the one in the example, the translate 
approach is slower unless the used in a loop
where the translation table is already built.

BTW, the PEP itself is not primary documentation
for users.  It is meant to document the design
discussion only.  

Feel free to post your recipe on ASPN or on
the newsgroup.
History
Date User Action Args
2010-10-23 21:55:08rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg119477
2010-10-23 16:35:49samwysesetmessages: + msg119445
2010-10-23 14:30:25r.david.murraysetnosy: + ncoghlan, rhettinger, eric.smith, r.david.murray
messages: + msg119429
2010-10-23 14:05:24samwysecreate