I had come across two separate modules that did something on the same lines.<div>one is 'pypp' and the other is 'preprocess.py' from ActiveState.</div><div><br></div><div>in the former, if the file contains a comment '#pypp" somewhere in the file...it gets preprocessed....but it only uses MakoTemplates for preprocessing..and so you need to have Mako on your machine.</div>
<div><br></div><div>the later, preprocess.py is a script that looks for specific tagged comments like #ifdef, #ifndef etc and creates another file...after preprocessing.</div><div><br></div><div>A marriage between the two seems to be this Imputil thing from Paul McGuire :))</div>
<div><br></div><div>One of the things that needs to be done...is, if a upper layer import is working like this:</div><div><br></div><div># ---- topMod.py ---</div><div>import something.onething as one</div><div>import something.anotherthing as another</div>
<div><br></div><div>And 'onething.py' and 'anotherthing.py' contain the preprocessor directives...the new file that gets made and is loaded....should now be loaded with their respective module names(i.e. 'one' and 'another'). Need to do something for this....</div>
<div><br></div><div>Python is certainly...cool.</div><div><br></div><div>Enjoy,</div><div>Vishal</div><div><br><div class="gmail_quote">On Wed, Jun 17, 2009 at 1:29 AM, Vishal <span dir="ltr"><<a href="mailto:vsapre80@gmail.com">vsapre80@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">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"><div class="im">On Tue, Jun 16, 2009 at 11:57 AM, Jeff Rush <span dir="ltr"><<a href="mailto:jeff@taupro.com" target="_blank">jeff@taupro.com</a>></span> wrote:<br>
</div><div><div></div><div class="h5"><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>> ?* 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>_______________________________________________<br>
BangPypers mailing list<br>
<a href="mailto:BangPypers@python.org" target="_blank">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></div></div><br><br clear="all"><div class="im"><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></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>