Author cheryl.sabella
Recipients Chris.Barker, ChrisBarker, Gaurav Tatke, Jim.Jewett, cheryl.sabella, docs@python, rhettinger, serhiy.storchaka
Date 2018-03-13.23:47:11
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1520984832.08.0.467229070634.issue28612@psf.upfronthosting.co.za>
In-reply-to
Content
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?
History
Date User Action Args
2018-03-13 23:47:12cheryl.sabellasetrecipients: + cheryl.sabella, rhettinger, Chris.Barker, docs@python, Jim.Jewett, serhiy.storchaka, ChrisBarker, Gaurav Tatke
2018-03-13 23:47:12cheryl.sabellasetmessageid: <1520984832.08.0.467229070634.issue28612@psf.upfronthosting.co.za>
2018-03-13 23:47:12cheryl.sabellalinkissue28612 messages
2018-03-13 23:47:11cheryl.sabellacreate