-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
str.translate needs a mapping example #72798
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
Comments
One commonly needed string transformation is stripping out certain characters (or only keeping certain characters). This is common enough that it might be worth a dedicated method, except, that, as Stephen J. Turnbull wrote in https://mail.python.org/pipermail/python-ideas/2016-November/043501.html """ Alas, while inspired, it isn't obvious to someone who isn't yet used to the power of python custom classes. The documentation (such as https://docs.python.org/3/library/stdtypes.html?highlight=translate#str.translate ) should include such an example. One possible example would be a defaultdict that says to discard any characters except lower case ASCII lettersI. |
https://mail.python.org/pipermail/python-ideas/2016-November/043539.html by Chris Barker points out that a custom object (which doesn't ever store the missing "keys") may be better still... though I'm not sure it is better enough to complicate the docs. |
Agreed: the custom dict type would be nice for a recipe or blog post or... but not for the docs. I'll note that the other trick to this recipe is that you need to know to use lambda to make a "None factory" for defaultdict -- though maybe that's a ToDo for the defaultdict docs... |
Hi, I am new to Python and want to contribute. I am attaching a patch having required example of using defaultdict with translate. Please let me know if anything needs to be changed. I have tested the example and also the html doc in my local. Regards, |
I like the idea of adding a mapping example but don't want to encourage use of defaultdict in contexts like this one. A defaultdict usefully specifies a default but has the unpleasant side-effect of altering the dictionary (adding new keys) during the look-up phase. This has bitten a lot of people (including famous ones like Peter Norvig). |
If the side effect of defaultdict is unpleasant, the correct way is combining the translation mapping with the custom mapping by ChainMap. But this example is too complex for the documentation of str.translate(). On other side, it is trivial for more experience users and don't need special mentioning. I think other resources (ActiveState Code Reciepes [1] or books) are better places for this example. |
Hi, Pardon my ignorance, I am new to this but have below queries/thoughts -
Regards, |
Should a user be suggested to use str.translate() for the use case where user only wants to keep certain characters and strip off everything else? |
This all came out of a thread on python-ideas, starting here: https://mail.python.org/pipermail/python-ideas/2016-October/043284.html the thread kind of petered out, but it seems there was a kinda-sorta And the defaultdict method was proposed as the easiest / most pythonic. As it happens, I did't live the fact hat defaultdict will build up a class NoneDict(dict):
"""
Dictionary implementation that always returns None when a key is not in
the dict,
rather than raising a KeyError
"""
def __getitem__(self, key):
try:
val = dict.__getitem__(self, key)
except KeyError:
val = None
return val Though maybe that's a bit much for the docs. However, in short: either the defaultdict approach is siple and pythonic enough to be in teh (or maybe someone has another nifty pythonic way to do this with the stdlib -CHB On Fri, Dec 30, 2016 at 12:18 PM, Gaurav Tatke <report@bugs.python.org>
-- Christopher Barker, Ph.D. Emergency Response Division |
IDLE just added similar functionality to pyparse (bpo-32940) using: class ParseMap(dict):
def __missing__(self, key):
return 120 # ord('x')
# Map all ascii to 120 to avoid __missing__ call, then replace some.
trans = ParseMap.fromkeys(range(128), 120)
trans.update((ord(c), ord('(')) for c in "({[") # open brackets => '(';
trans.update((ord(c), ord(')')) for c in ")}]") # close brackets => ')'.
trans.update((ord(c), ord(c)) for c in "\"'\\\n#") # Keep these.
code = code.translate(trans) Of course, all that is probably too much for a docs example, but it uses a mapping without the side effect of defaultdict. I wonder if defining the dict subclass with __missing__ and then the example of keeping only lowercase letters would work for the docs? |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: