Cheryl Sabella <> added the comment:

IDLE just added similar functionality to pyparse (issue 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?

nosy: +csabella

Python tracker <>
Python-bugs-list mailing list

Reply via email to