Cool....<div>While I was using preprocessor directives in C/C++...the usual internet rant I&#39;ve always heard, is &quot;preprocessor is bad, using preprocessor is bad habit.&quot;. Somehow I feel that preprocessing is not all that bad. It can and does solve important problems. May be these rants mostly referred to the &#39;macro&#39; side of things...</div>
<div><br><div>Thanks a lot Jeff, this was really informative.</div><div><br></div><div>Vishal</div><div><br><div class="gmail_quote">On Tue, Jun 16, 2009 at 11:57 AM, Jeff Rush <span dir="ltr">&lt;<a href="mailto:jeff@taupro.com">jeff@taupro.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Vishal wrote:<br>
&gt;<br>
&gt; *Is there a way to create a conditionally compilable Python script<br>
<div class="im">&gt; ?* Some facility that would prevent from code getting compiled into<br>
&gt; &quot;.pyc&quot;....something like the old #ifdef #endif preprocessor<br>
&gt; directives....is there a Python preprocessory available :)<br>
<br>
</div>I&#39;ve given this more thought.  In the April 2009 issue of Python<br>
Magazine is a wonderful article by Paul McGuire on writing Domain<br>
Specific Languages.  It shows how to intercept the import of specific<br>
types of files to preprocess the source before compiling it into<br>
bytecode.  I played with those ideas to see what could be done for your<br>
case.<br>
<br>
So say you had a module marked up with special commenting like:<br>
<br>
--- cut here --- abc.pyp<br>
def main():<br>
    print &quot;Hello&quot;  #? DEBUG<br>
    print &quot;There&quot;  #? DEBUG2<br>
    print &quot;World&quot;<br>
--- cut here --- abc.pyp<br>
<br>
Then in your main module you could do the following:<br>
<br>
--- cut here --- demo.py<br>
import cond_importer # can put in site.py for global application<br>
<br>
import abc<br>
abc.main()<br>
--- cut here --- demo.py<br>
<br>
Now you run the program normally and get:<br>
<br>
$ python2.5 demo.py<br>
Hello<br>
There<br>
World<br>
<br>
or you can filter out lines using:<br>
<br>
$ EXCLUDES=DEBUG python2.5 demo.py<br>
There<br>
World<br>
<br>
or even:<br>
<br>
$ EXCLUDES=DEBUG:DEBUG2 python2.5 demo.py<br>
World<br>
<br>
The magic to make this happen is just:<br>
<br>
--- cut here --- cond_import.py<br>
from __future__ import with_statement # for Python &lt; 2.6<br>
<br>
import os<br>
import imputil<br>
import re<br>
<br>
patt = re.compile(r&quot;.*#\? (?P&lt;tag&gt;[_a-zA-Z][_a-zA-Z0-9]*)$&quot;)<br>
<br>
try: # construct a set of tags to exclude in .py source<br>
    pyexcludes = frozenset(os.environ[&#39;EXCLUDES&#39;].split(&#39;:&#39;))<br>
except Exception:<br>
    pyexcludes = frozenset()<br>
<br>
def preprocess_source(filepath, fileinfo, filename):<br>
<br>
    src = []<br>
    with file(filepath, &#39;r&#39;) as f:<br>
        for line in f:<br>
            m = patt.match(line)<br>
            if m is None or m.group(&#39;tag&#39;) not in pyexcludes:<br>
                src.append(line)<br>
<br>
        src = &#39;\n&#39;.join(src)<br>
        codeobj = compile(src, filepath, &#39;exec&#39;)<br>
<br>
    # create return tuple:<br>
    #   import flag (0=module, 1=package)<br>
    #   code object<br>
    #   dictionary of variable definitions<br>
    return 0, codeobj, {}<br>
<br>
importer = imputil.ImportManager()<br>
importer.add_suffix(&#39;.pyp&#39;, preprocess_source)<br>
importer.install()<br>
--- cut here --- cond_import.py<br>
<br>
I arbitrarily chose a file extension of .pyp for files to preprocess but<br>
 you could use anything.  And the tags to exclude could instead be tags<br>
to include.  For handling packages (dirs) instead of modules you&#39;d have<br>
to add a test for file or directory and change the code a bit.<br>
<br>
Anyway it raises some interesting possibilities of preprocessing.<br>
Python is just so cool.<br>
<font color="#888888"><br>
-Jeff<br>
</font><div><div></div><div class="h5">_______________________________________________<br>
BangPypers mailing list<br>
<a href="mailto:BangPypers@python.org">BangPypers@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/bangpypers" target="_blank">http://mail.python.org/mailman/listinfo/bangpypers</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Thanks and best regards,<br>Vishal Sapre<br><br>---<br><br>&quot;So say...Day by day, in every way, I am getting better, better and better !!!&quot;<br>&quot;A Strong and Positive attitude creates more miracles than anything else. Because...Life is 10% how you make it, and 90% how you take it&quot;<br>
&quot;Diamond is another piece of coal that did well under pressure”<br>&quot;Happiness keeps u Sweet, Trials keep u Strong, <br>Sorrow keeps u Human, Failure Keeps u Humble, <br>Success keeps u Glowing, But only God Keeps u Going.....Keep Going.....&quot;<br>

</div></div>