Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #53674

Re: how to detect comment in source code file ?

Date 2013-09-05 07:33 +0200
From Vincent Vande Vyvre <vincent.vandevyvre@swing.be>
Subject Re: how to detect comment in source code file ?
References <1fbab13d-bb98-465e-affe-f2fddeec8138@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.77.1378359658.5461.python-list@python.org> (permalink)

Show all headers | View raw


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 <https://launchpad.net/oqapy> . Qarte 
<https://launchpad.net/qarte> . PaQager <https://launchpad.net/paqager>

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

how to detect comment in source code file ? vnkumbhani@gmail.com - 2013-09-03 23:05 -0700
  Re: how to detect comment in source code file ? Ben Finney <ben+python@benfinney.id.au> - 2013-09-04 17:02 +1000
    Re: how to detect comment in source code file ? Paul Rudin <paul@rudin.co.uk> - 2013-09-04 08:18 +0100
    Re: how to detect comment in source code file ? Steven D'Aprano <steve@pearwood.info> - 2013-09-05 01:56 +0000
  Re: how to detect comment in source code file ? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-09-04 23:40 -0400
    Re: how to detect comment in source code file ? Piet van Oostrum <piet@vanoostrum.org> - 2013-09-05 08:11 -0400
  Re: how to detect comment in source code file ? Vincent Vande Vyvre <vincent.vandevyvre@swing.be> - 2013-09-05 07:33 +0200

csiph-web