Cool....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...
Thanks a lot Jeff, this was really informative. Vishal On Tue, Jun 16, 2009 at 11:57 AM, Jeff Rush <j...@taupro.com> wrote: > Vishal wrote: > > > > *Is there a way to create a conditionally compilable Python script > > ?* Some facility that would prevent from code getting compiled into > > ".pyc"....something like the old #ifdef #endif preprocessor > > directives....is there a Python preprocessory available :) > > I've given this more thought. In the April 2009 issue of Python > Magazine is a wonderful article by Paul McGuire on writing Domain > Specific Languages. It shows how to intercept the import of specific > types of files to preprocess the source before compiling it into > bytecode. I played with those ideas to see what could be done for your > case. > > So say you had a module marked up with special commenting like: > > --- cut here --- abc.pyp > def main(): > print "Hello" #? DEBUG > print "There" #? DEBUG2 > print "World" > --- cut here --- abc.pyp > > Then in your main module you could do the following: > > --- cut here --- demo.py > import cond_importer # can put in site.py for global application > > import abc > abc.main() > --- cut here --- demo.py > > Now you run the program normally and get: > > $ python2.5 demo.py > Hello > There > World > > or you can filter out lines using: > > $ EXCLUDES=DEBUG python2.5 demo.py > There > World > > or even: > > $ EXCLUDES=DEBUG:DEBUG2 python2.5 demo.py > World > > The magic to make this happen is just: > > --- cut here --- cond_import.py > from __future__ import with_statement # for Python < 2.6 > > import os > import imputil > import re > > patt = re.compile(r".*#\? (?P<tag>[_a-zA-Z][_a-zA-Z0-9]*)$") > > try: # construct a set of tags to exclude in .py source > pyexcludes = frozenset(os.environ['EXCLUDES'].split(':')) > except Exception: > pyexcludes = frozenset() > > def preprocess_source(filepath, fileinfo, filename): > > src = [] > with file(filepath, 'r') as f: > for line in f: > m = patt.match(line) > if m is None or m.group('tag') not in pyexcludes: > src.append(line) > > src = '\n'.join(src) > codeobj = compile(src, filepath, 'exec') > > # create return tuple: > # import flag (0=module, 1=package) > # code object > # dictionary of variable definitions > return 0, codeobj, {} > > importer = imputil.ImportManager() > importer.add_suffix('.pyp', preprocess_source) > importer.install() > --- cut here --- cond_import.py > > I arbitrarily chose a file extension of .pyp for files to preprocess but > you could use anything. And the tags to exclude could instead be tags > to include. For handling packages (dirs) instead of modules you'd have > to add a test for file or directory and change the code a bit. > > Anyway it raises some interesting possibilities of preprocessing. > Python is just so cool. > > -Jeff > _______________________________________________ > BangPypers mailing list > BangPypers@python.org > http://mail.python.org/mailman/listinfo/bangpypers > -- Thanks and best regards, Vishal Sapre --- "So say...Day by day, in every way, I am getting better, better and better !!!" "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" "Diamond is another piece of coal that did well under pressureā "Happiness keeps u Sweet, Trials keep u Strong, Sorrow keeps u Human, Failure Keeps u Humble, Success keeps u Glowing, But only God Keeps u Going.....Keep Going....."
_______________________________________________ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers