[issue10178] PEP 378 uses replace where translate may work better

2010-10-23 Thread Raymond Hettinger

Raymond Hettinger  added the comment:

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.

--
resolution:  -> rejected
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10178] PEP 378 uses replace where translate may work better

2010-10-23 Thread samwyse

samwyse  added the comment:

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.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10178] PEP 378 uses replace where translate may work better

2010-10-23 Thread R. David Murray

R. David Murray  added the comment:

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.

--
nosy: +eric.smith, ncoghlan, r.david.murray, rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10178] PEP 378 uses replace where translate may work better

2010-10-23 Thread samwyse

New submission from samwyse :

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

--
assignee: d...@python
components: Documentation
messages: 119427
nosy: d...@python, samwyse
priority: normal
severity: normal
status: open
title: PEP 378 uses replace where translate may work better
type: behavior
versions: Python 2.7, Python 3.1

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com