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


Groups > comp.lang.python > #53598 > unrolled thread

how to detect comment in source code file ?

Started byvnkumbhani@gmail.com
First post2013-09-03 23:05 -0700
Last post2013-09-05 07:33 +0200
Articles 7 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  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

#53598 — how to detect comment in source code file ?

Fromvnkumbhani@gmail.com
Date2013-09-03 23:05 -0700
Subjecthow to detect comment in source code file ?
Message-ID<1fbab13d-bb98-465e-affe-f2fddeec8138@googlegroups.com>
how works python interpreter for finding comment ?
if possible find nested comment ?

[toc] | [next] | [standalone]


#53600

FromBen Finney <ben+python@benfinney.id.au>
Date2013-09-04 17:02 +1000
Message-ID<mailman.27.1378278177.5461.python-list@python.org>
In reply to#53598
vnkumbhani@gmail.com writes:

> how works python interpreter for finding comment ?

It works as specified in the language reference. In particular, see
<URL:http://docs.python.org/3/reference/lexical_analysis.html#comments>.

> if possible find nested comment ?

Python's comments are line-end comments only. The syntax does not allow
multi-line nor nested comments.

-- 
 \       “If you go flying back through time and you see somebody else |
  `\   flying forward into the future, it's probably best to avoid eye |
_o__)                                           contact.” —Jack Handey |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#53608

FromPaul Rudin <paul@rudin.co.uk>
Date2013-09-04 08:18 +0100
Message-ID<87a9jtoy0w.fsf@no-fixed-abode.cable.virginmedia.net>
In reply to#53600
Ben Finney <ben+python@benfinney.id.au> writes:

> vnkumbhani@gmail.com writes:
>
>> how works python interpreter for finding comment ?
>
> It works as specified in the language reference. In particular, see
> <URL:http://docs.python.org/3/reference/lexical_analysis.html#comments>.
>
>> if possible find nested comment ?
>
> Python's comments are line-end comments only. The syntax does not allow
> multi-line nor nested comments.

Although you can use unbound multi-line strings as a kind of
comment. But its often better to make the strings into doc strings
proper.

[toc] | [prev] | [next] | [standalone]


#53662

FromSteven D'Aprano <steve@pearwood.info>
Date2013-09-05 01:56 +0000
Message-ID<5227e4e1$0$2743$c3e8da3$76491128@news.astraweb.com>
In reply to#53600
On Wed, 04 Sep 2013 17:02:42 +1000, Ben Finney wrote:

> vnkumbhani@gmail.com writes:
> 
>> how works python interpreter for finding comment ?
> 
> It works as specified in the language reference. In particular, see
> <URL:http://docs.python.org/3/reference/lexical_analysis.html#comments>.
> 
>> if possible find nested comment ?
> 
> Python's comments are line-end comments only. The syntax does not allow
> multi-line nor nested comments.

While that's technically true, you can use bare strings as de facto 
comments. The compiler drops any bare strings it sees, so one can nest 
pseudo-comments like this:


do_this()
"""
do_that()
do_something()  # Make the widget work correctly.
'''
for x in range(5):
    do_something_else()
'''
do_something_different()
"""


In the above, everything except do_this() is commented out by being 
turned into a string.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#53667

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2013-09-04 23:40 -0400
Message-ID<mailman.70.1378352705.5461.python-list@python.org>
In reply to#53598
On Tue, 3 Sep 2013 23:05:57 -0700 (PDT), vnkumbhani@gmail.com declaimed the
following:

>how works python interpreter for finding comment ?
>if possible find nested comment ?

	Python does not have "nested comment". Comments begin at any #
character that is not inside a string (something inside ' or " pairs).
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#53695

FromPiet van Oostrum <piet@vanoostrum.org>
Date2013-09-05 08:11 -0400
Message-ID<m2vc2fqxh4.fsf@cochabamba.vanoostrum.org>
In reply to#53667
Dennis Lee Bieber <wlfraed@ix.netcom.com> writes:

> On Tue, 3 Sep 2013 23:05:57 -0700 (PDT), vnkumbhani@gmail.com declaimed the
> following:
>
>>how works python interpreter for finding comment ?
>>if possible find nested comment ?
>
> 	Python does not have "nested comment". Comments begin at any #
> character that is not inside a string (something inside ' or " pairs).

You could consider this a kind of nested comment :)

# if condition:
#     # calculate the amount
#     amount = sum(parts)
-- 
Piet van Oostrum <piet@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]

[toc] | [prev] | [next] | [standalone]


#53674

FromVincent Vande Vyvre <vincent.vandevyvre@swing.be>
Date2013-09-05 07:33 +0200
Message-ID<mailman.77.1378359658.5461.python-list@python.org>
In reply to#53598
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>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web