Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!news2.arglkargh.de!dedekind.zen.co.uk!zen.net.uk!hamilton.zen.co.uk!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'importing': 0.05; 'interpreter': 0.05; 'url:launchpad': 0.05; 'column': 0.07; 'nested': 0.07; 'subject:code': 0.07; 'subject:file': 0.07; '"if': 0.09; 'python': 0.11; 'def': 0.12; "%s'": 0.16; 'from:addr:swing.be': 0.16; 'from:addr:vincent.vandevyvre': 0.16; 'from:name:vincent vande vyvre': 0.16; 'message-id:@swing.be': 0.16; 'oqapy': 0.16; 'paqager': 0.16; 'received:mobistar.be': 0.16; 'stringio': 0.16; 'subject:comment': 0.16; 'tokenize': 0.16; 'tokens:': 0.16; 'url:oqapy': 0.16; 'url:paqager': 0.16; 'url:qarte': 0.16; 'v.v.': 0.16; '\xe9crit': 0.16; 'subject: ?': 0.16; 'module': 0.19; 'import': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; 'script': 0.25; '(for': 0.26; 'header:In-Reply- To:1': 0.27; 'comment': 0.34; 'false': 0.36; 'possible': 0.36; 'auto': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:- 60': 0.39; 'how': 0.40; 'email addr:gmail.com': 0.63; 'subject:source': 0.84; 'yourself,': 0.95 Date: Thu, 05 Sep 2013 07:33:18 +0200 From: Vincent Vande Vyvre User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130803 Thunderbird/17.0.8 MIME-Version: 1.0 To: python-list@python.org Subject: Re: how to detect comment in source code file ? References: <1fbab13d-bb98-465e-affe-f2fddeec8138@googlegroups.com> In-Reply-To: <1fbab13d-bb98-465e-affe-f2fddeec8138@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1378359658 news.xs4all.nl 15865 [2001:888:2000:d::a6]:35742 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:53674 Le 04/09/2013 08:05, vnkumbhani@gmail.com a écrit : > how works python interpreter for finding comment ? > if possible find nested comment ? If you need to find it yourself, you can use the module tokenize. ex.: --------------------------------------------------------------- import tokenize from StringIO import StringIO script = "# importing comment\nfrom foo import baz"\ "if baz.vers < 2:\n AUTO = False # Retrocompatibility" def find_comment(): print script cmt = tokenize.COMMENT tokens = tokenize.generate_tokens(StringIO(script).readline) for typ, _, begin, _, _ in tokens: if typ == cmt: print 'Find comment line %s at column %s' % begin find_comment() --------------------------------------------------------------- (for Python 3 import StringIO from io) -- Vincent V.V. Oqapy . Qarte . PaQager