Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PEP 378 uses replace where translate may work better #54387

Closed
samwyse mannequin opened this issue Oct 23, 2010 · 4 comments
Closed

PEP 378 uses replace where translate may work better #54387

samwyse mannequin opened this issue Oct 23, 2010 · 4 comments
Labels
docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error

Comments

@samwyse
Copy link
Mannequin

samwyse mannequin commented Oct 23, 2010

BPO 10178
Nosy @rhettinger, @ncoghlan, @ericvsmith, @bitdancer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2010-10-23.21:55:08.782>
created_at = <Date 2010-10-23.14:05:24.043>
labels = ['type-bug', 'docs']
title = 'PEP 378 uses replace where translate may work better'
updated_at = <Date 2010-10-23.21:55:08.781>
user = 'https://bugs.python.org/samwyse'

bugs.python.org fields:

activity = <Date 2010-10-23.21:55:08.781>
actor = 'rhettinger'
assignee = 'docs@python'
closed = True
closed_date = <Date 2010-10-23.21:55:08.782>
closer = 'rhettinger'
components = ['Documentation']
creation = <Date 2010-10-23.14:05:24.043>
creator = 'samwyse'
dependencies = []
files = []
hgrepos = []
issue_num = 10178
keywords = []
message_count = 4.0
messages = ['119427', '119429', '119445', '119477']
nosy_count = 6.0
nosy_names = ['rhettinger', 'ncoghlan', 'eric.smith', 'samwyse', 'r.david.murray', 'docs@python']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue10178'
versions = ['Python 3.1', 'Python 2.7']

@samwyse
Copy link
Mannequin Author

samwyse mannequin commented Oct 23, 2010

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

@samwyse samwyse mannequin assigned docspython Oct 23, 2010
@samwyse samwyse mannequin added docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error labels Oct 23, 2010
@bitdancer
Copy link
Member

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.

@samwyse
Copy link
Mannequin Author

samwyse mannequin commented Oct 23, 2010

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.

@rhettinger
Copy link
Contributor

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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants