[BangPypers] How to create Debug and Release code in Python

Vishal vsapre80 at gmail.com
Tue Jun 16 22:09:42 CEST 2009


I had come across two separate modules that did something on the same lines.one
is 'pypp' and the other is 'preprocess.py' from ActiveState.

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.

the later, preprocess.py is a script that looks for specific tagged comments
like #ifdef, #ifndef etc and creates another file...after preprocessing.

A marriage between the two seems to be this Imputil thing from Paul McGuire
:))

One of the things that needs to be done...is, if a upper layer import is
working like this:

# ---- topMod.py ---
import something.onething as one
import something.anotherthing as another

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....

Python is certainly...cool.

Enjoy,
Vishal

On Wed, Jun 17, 2009 at 1:29 AM, Vishal <vsapre80 at gmail.com> wrote:

> 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 <jeff at 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 at 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....."
>



-- 
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....."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/bangpypers/attachments/20090617/734da30c/attachment-0001.htm>


More information about the BangPypers mailing list