Cool....<div>While I was using preprocessor directives in C/C++...the usual internet rant I've always heard, is "preprocessor is bad, using preprocessor is bad habit.". 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 'macro' 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"><<a href="mailto:jeff@taupro.com">jeff@taupro.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Vishal wrote:<br>
><br>
> *Is there a way to create a conditionally compilable Python script<br>
<div class="im">> ?* Some facility that would prevent from code getting compiled into<br>
> ".pyc"....something like the old #ifdef #endif preprocessor<br>
> directives....is there a Python preprocessory available :)<br>
<br>
</div>I'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 "Hello" #? DEBUG<br>
print "There" #? DEBUG2<br>
print "World"<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 < 2.6<br>
<br>
import os<br>
import imputil<br>
import re<br>
<br>
patt = re.compile(r".*#\? (?P<tag>[_a-zA-Z][_a-zA-Z0-9]*)$")<br>
<br>
try: # construct a set of tags to exclude in .py source<br>
pyexcludes = frozenset(os.environ['EXCLUDES'].split(':'))<br>
except Exception:<br>
pyexcludes = frozenset()<br>
<br>
def preprocess_source(filepath, fileinfo, filename):<br>
<br>
src = []<br>
with file(filepath, 'r') as f:<br>
for line in f:<br>
m = patt.match(line)<br>
if m is None or m.group('tag') not in pyexcludes:<br>
src.append(line)<br>
<br>
src = '\n'.join(src)<br>
codeobj = compile(src, filepath, 'exec')<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('.pyp', 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'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>"So say...Day by day, in every way, I am getting better, better and better !!!"<br>"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"<br>
"Diamond is another piece of coal that did well under pressure”<br>"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....."<br>
</div></div>