Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Conditional compilation

0 views
Skip to first unread message

jb

unread,
May 10, 2002, 8:14:17 AM5/10/02
to
This question was probably asked many times.

It does not seem to be possible to "comment out" blocks of code, which would
be a very nice feature for testing purposes. (Maybe I can do it with """
but that looks a bit cumbersome.)

Any ideas, hint, proposals?

--
Js Bi

"Il n'y a guère dans la vie qu'une préoccupation grave: c'est la mort;"
(Dumas)

-----------== Posted via Newsgroups.Com - Uncensored Usenet News ==----------
http://www.newsgroups.com The #1 Newsgroup Service in the World!
-----= Over 100,000 Newsgroups - Ulimited Fast Downloads - 19 Servers =-----

Martin v. Löwis

unread,
May 10, 2002, 9:13:14 AM5/10/02
to
jb <jbl...@hotmail.com> writes:

> This question was probably asked many times.
>
> It does not seem to be possible to "comment out" blocks of code, which would
> be a very nice feature for testing purposes. (Maybe I can do it with """
> but that looks a bit cumbersome.)
>
> Any ideas, hint, proposals?

The typical solution is to put the entire block in a "if 0:"
statement. If your editor does not conveniently allow to right-indent
a block of code, you may succeed with putting the if statement on the
half indentation level; you'll need to add 'if 1:' for the rest of the
block in this case, also.

HTH,
Martin

holger krekel

unread,
May 10, 2002, 8:42:52 AM5/10/02
to
James J. Besemer wrote:

> jb wrote:
>
> > It does not seem to be possible to "comment out" blocks of code, which would
> > be a very nice feature for testing purposes. (Maybe I can do it with """
> > but that looks a bit cumbersome.)
>
> Prefix your code segment with
>
> if 0:
>
> and indent it a level.

""" and 'if 0:' are the standard possibilities. I guess one would
need a truly developer oriented IDE to implement this indepently from
language (handling say a all-included-file and a work-file separetely).

Can't emacs or vi do folding/collapsing several lines into one line
and allow writing out non-folded lines only?

> --jb

jb answers jb. maybe i am joining a monologue?

:-),
holger


James J. Besemer

unread,
May 10, 2002, 8:27:04 AM5/10/02
to

jb wrote:

> This question was probably asked many times.
>
> It does not seem to be possible to "comment out" blocks of code, which would
> be a very nice feature for testing purposes. (Maybe I can do it with """
> but that looks a bit cumbersome.)
>
> Any ideas, hint, proposals?

Prefix your code segment with

if 0:

and indent it a level.

This is not as fully general as #ifdef but since even declarations are executed
at runtime, it'll serve as a way to selectively include or exclude blocks of
code.

Regards

--jb

--
James J. Besemer 503-280-0838 voice
http://cascade-sys.com 503-280-0375 fax
mailto:j...@cascade-sys.com


Fernando Pérez

unread,
May 10, 2002, 12:13:29 PM5/10/02
to
James J. Besemer wrote:

> Prefix your code segment with
>
> if 0:
>
> and indent it a level.
>
> This is not as fully general as #ifdef but since even declarations are
> executed at runtime, it'll serve as a way to selectively include or exclude
> blocks of code.
>

Why not just use #ifdef? ;) I'm only half joking: it would take a few minutes
to whip up a shell script to run your python code through the gcc
preprocessor first before passing it to python. As long as you have spaces in
your normal comments (no #include as a comment), you should be allright, no?
I haven't actually tried it, so maybe the preprocessor would somehow get
confused, and yes, it's a bit of a twisted idea. But fun ;)

cheers,

f.

Terry Reedy

unread,
May 10, 2002, 3:51:52 PM5/10/02
to

"jb" <jbl...@hotmail.com> wrote in message
news:3cdbb...@news3.newsgroups.com...

> This question was probably asked many times.
>
> It does not seem to be possible to "comment out" blocks of code,
which would
> be a very nice feature for testing purposes. (Maybe I can do it with
"""
> but that looks a bit cumbersome.)

Typing \n''' twice is hardly any more cumbersome than typing \n/*,
\n*/.
The ease of repeats compesates for the two extra keystrokes.

Terry J. Reedy

John J. Lee

unread,
May 10, 2002, 6:14:12 PM5/10/02
to

[...]

C-c # to comment, C-c rk to uncomment (kill rectangle, to be specific), in
emacs.


John

Bengt Richter

unread,
May 10, 2002, 9:57:49 PM5/10/02
to

Need to scan for ''' in the "commented-out" part though.

Regards,
Bengt Richter

holger krekel

unread,
May 11, 2002, 8:13:51 AM5/11/02
to
John J. Lee wrote:
> > """ and 'if 0:' are the standard possibilities. I guess one would
> > need a truly developer oriented IDE to implement this indepently from
> > language (handling say a all-included-file and a work-file separetely).
> >
> > Can't emacs or vi do folding/collapsing several lines into one line
> > and allow writing out non-folded lines only?
> [...]
>
> C-c # to comment, C-c rk to uncomment (kill rectangle, to be specific), in
> emacs.


thanks. but this doesn't do 'collapsing' several lines into temporary
nothingness.

holger


John J. Lee

unread,
May 12, 2002, 11:01:06 AM5/12/02
to

Sorry, was really replying to the thread in general rather than your
specific Q.; but I think emacs' does have a minor mode to do this (can't
remember what it's called, so this may be disinformation: could be a major
mode, in which case you can't mix it w/ python-mode of course).


John

Laura Creighton

unread,
May 12, 2002, 3:06:37 PM5/12/02
to
>
> thanks. but this doesn't do 'collapsing' several lines into temporary
> nothingness.
>
> holger

I missed the start of this, but, is this what you want? Courtesy
Ronny Wikh rw @ strakt.com

This we use to make whole classes go away so you can see program structure.

Laura
---------
;;
;; python outline mode
;;

(defvar outline-start-hidden t "Start outline hidden")

(defun outline-setup (regexp)
"Setup outline mode"
(defvar outline-toggle-all-flag nil "toggle all flag")
(defvar cpos_save nil "current cursor position")
(outline-minor-mode)
(setq outline-regexp regexp)
(define-key outline-minor-mode-map "\C-c\C-e" 'outline-toggle-entry)
(define-key outline-minor-mode-map "\C-c\C-a" 'outline-toggle-all)
(if outline-start-hidden
(hide-body))

(defun outline-toggle-entry () (interactive)
"Toggle outline hiding for the entry under the cursor"
(if (progn
(setq cpos_save (point))
(end-of-line)
(get-char-property (point) 'invisible))
(progn
(show-subtree)
(goto-char cpos_save))
(progn
(hide-leaves)
(goto-char cpos_save))))

(defun outline-toggle-all () (interactive)
"Toggle outline hiding for the entire file"
(if outline-toggle-all-flag
(progn
(setq outline-toggle-all-flag nil)
(hide-body))
(progn
(setq outline-toggle-all-flag t)
(show-all))))
)

(defun python-outline () (interactive)
"Python outline mode"
(python-mode)
(outline-setup "^class \\|[ ]*def \\|^#"))

(defun texi-outline () (interactive)
"Texinfo outline mode"
(texinfo-mode)
(outline-setup "^@chap\\|@\\(sub\\)*section"))


Garth T Kidd

unread,
May 12, 2002, 8:36:36 PM5/12/02
to
> It does not seem to be possible to "comment out" blocks of code, which would
> be a very nice feature for testing purposes. (Maybe I can do it with """
> but that looks a bit cumbersome.)
>
> Any ideas, hint, proposals?

One thing I love in UserTalk (of Frontier, Manilla and Radio UserLand
fame) is the 'bundle' keyword that simply provides another variable
scope and bundles up a few lines of code.

In the Frontier/Radio environment all programming is done in an
outliner, and bundling turns out to be a whole lot nicer than it
originally looks -- so much so, that these days I curse as much about
there being no 'bundle' in Python as I do about most Python features
lacking in UserTalk. :)

Compare::

# do this

... code to do this

# do that

... code to do that

With::

bundle # do this
... code to do this

bundle # do that
... code to do that

I'm almost tested to hack it:

if 1: # do this
... code to do this

... but that's a little ghastly. If I could figure out a way to add a
keyword in pure Python, I'd do it. :)

Regards,
Garth.

Kragen Sitaker

unread,
May 13, 2002, 7:34:35 PM5/13/02
to
Fernando Pérez <fper...@yahoo.com> writes:

> James J. Besemer wrote:
> > This is not as fully general as #ifdef but since even declarations are
>
> Why not just use #ifdef? ;)

cpp will choke on strings spanning multiple lines; it knows too much
about C syntax. You could use m4.

My usual way to comment out a block is to highlight it and type
control-C #, which comments out the block. But I use Emacs.

Peter Hansen

unread,
May 13, 2002, 7:52:05 PM5/13/02
to
Garth T Kidd wrote:
>
> One thing I love in UserTalk (of Frontier, Manilla and Radio UserLand
> fame) is the 'bundle' keyword that simply provides another variable
> scope and bundles up a few lines of code.
>
> Compare::
>
> # do this
>
> ... code to do this
>
> # do that
>
> ... code to do that
>
> With::
>
> bundle # do this
> ... code to do this
>
> bundle # do that
> ... code to do that

I fail to see any advantage to that structure. Wouldn't this be
even better in any case? :

subroutineCallToDoThis()
subroutineCallToDoThat()

It's more concise, self-documenting, more modular, maintainable,
more testable, and so forth....

Just what value does "bundle" provide? Your example doesn't show it.

-Peter

0 new messages