Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; "'',": 0.07; 'string': 0.09; "'')": 0.09; 'arguments': 0.09; 'subject:Why': 0.09; 'uses.': 0.09; 'translation': 0.12; '(0,': 0.16; '(1,': 0.16; '(2,': 0.16; '(3,': 0.16; '-tkc': 0.16; '0),': 0.16; '4),': 0.16; '5),': 0.16; '6),': 0.16; 'cstringio': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'literals': 0.16; 'received:10.21': 0.16; 'stringio': 0.16; 'subject: \n ': 0.16; 'subject:comment': 0.16; 'subject:quoted': 0.16; 'subject:simple': 0.16; 'subject:triple': 0.16; 'token,': 0.16; 'tokenize': 0.16; 'wrote:': 0.18; 'module': 0.19; '>>>': 0.22; 'import': 0.22; 'print': 0.22; "haven't": 0.24; 'looks': 0.24; 'header:In-Reply- To:1': 0.27; 'code': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'run': 0.32; 'skip:t 40': 0.33; 'but': 0.35; 'charset:us-ascii': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'two': 0.37; 'received:10': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:t 30': 0.61; 'subject:. ': 0.67; 'played': 0.84 X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-MC-Relay: Neutral X-MailChannels-SenderId: wwwh|x-authuser|tim@thechases.com X-MailChannels-Auth-Id: wwwh X-MC-Loop-Signature: 1427413585468:2439036887 X-MC-Ingress-Time: 1427413585467 Date: Thu, 26 Mar 2015 18:47:37 -0500 From: Tim Chase To: python-list@python.org Subject: Re: A simple single line, triple-quoted comment is giving syntax error. Why? In-Reply-To: <55149305$0$13009$c3e8da3$5496439d@news.astraweb.com> References: <3533816.ZYnZ2OzjCs@PointedEars.de> <3971951.908YQu3oQO@PointedEars.de> <1504323.jUKfeKbQsP@PointedEars.de> <2362875.FIK8ImHTJA@PointedEars.de> <1582467.9xuzSGXesM@PointedEars.de> <55149305$0$13009$c3e8da3$5496439d@news.astraweb.com> X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AuthUser: tim@thechases.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1427414131 news.xs4all.nl 2858 [2001:888:2000:d::a6]:48839 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:88110 On 2015-03-27 10:15, Steven D'Aprano wrote: > If that's all it is, why don't you just run the tokenizer over it > and see what it says? > > py> from cStringIO import StringIO > py> code = StringIO('spam = "abcd" "efgh"\n') > py> import tokenize > py> for item in tokenize.generate_tokens(code.readline): > ... print item > ... > (1, 'spam', (1, 0), (1, 4), 'spam = "abcd" "efgh"\n') > (51, '=', (1, 5), (1, 6), 'spam = "abcd" "efgh"\n') > (3, '"abcd"', (1, 7), (1, 13), 'spam = "abcd" "efgh"\n') > (3, '"efgh"', (1, 14), (1, 20), 'spam = "abcd" "efgh"\n') > (4, '\n', (1, 20), (1, 21), 'spam = "abcd" "efgh"\n') > (0, '', (2, 0), (2, 0), '') > > > Looks to me that the two string literals each get their own token, Nice. I haven't played with the tokenize module before, but resolving arguments on comp.lang.python is one of the best possible uses. It was interesting to try other feeders to generate_tokens(), my favorite being >>> import tokenize >>> i = iter(["spam = 'abc' 'def'"]) >>> for item in tokenize.generate_tokens(lambda: next(i)): ... print(item) ... TokenInfo(type=1 (NAME), string='spam', start=(1, 0), end=(1, 4), line="spam = 'abc' 'def'") TokenInfo(type=52 (OP), string='=', start=(1, 5), end=(1, 6), line="spam = 'abc' 'def'") TokenInfo(type=3 (STRING), string="'abc'", start=(1, 7), end=(1, 12), line="spam = 'abc' 'def'") TokenInfo(type=3 (STRING), string="'def'", start=(1, 13), end=(1, 18), line="spam = 'abc' 'def'") TokenInfo(type=0 (ENDMARKER), string='', start=(2, 0), end=(2, 0), line='') It's also nice to have the translation from token-type to token-type-name in Py3 -tkc