I wrote a python preprocessor called pypreprocessor that does exactly what you are describing.
Source code and documentation are available on GitHub .
The package can also be downloaded / installed via PyPI .
Here is an example to accomplish what you are describing.
from pypreprocessor import pypreprocessor pypreprocessor.parse()
pypreprocessor is capable of much more than just preprocessing on the fly. To see more usage examples, check out the project on Google Code.
Update: More information about pypreprocessor
The way I do the preprocessing is simple. From the above example, the preprocessor imports the pypreprocessor object created in the pypreprocessor module. When you call parse () for the preprocessor, it independently consumes the file into which it is imported, and generates a temporary copy of itself that comments out all the preprocessor code (to avoid the preprocessor from recursively calling itself in an infinite loop) and comments on all unused parts.
Commenting out lines, as opposed to deleting them, is necessary to save line numbers in error messages if the module throws an exception or a failure occurs. And I even went so far as to rewrite the error trace so that the report would reflect the correct module file name that failed.
Then, the generated file containing the post-processed code is executed on the fly.
The advantage of using this method instead of simply adding a few built-in statements in your code is that you wonβt spend time evaluating useless statements because the commented parts of the code will be excluded from compiled .pyc files.
The downside (and my original reason for creating the module) is that you cannot run both Python 2x and Python 3x in the same file, because the pythons interpreter performs a full syntax check before executing the code and will reject any code of a particular version before The preprocessor is allowed to run :: sigh ::. My initial goal was to be able to develop 2x and 3x code in the same file, which would produce version-specific bytecode depending on what it was running on.
In any case, the preprocessor module is still very useful for implementing common c-style preprocessing capabilities. In addition, the preprocessor is able to output the post-processed code to a file for later use if you want.
In addition, if you want to generate a version that contains all preprocessor directives, as well as excluded #ifdefs, removing it is as simple as setting a flag in the preprocessor code before calling parse (). This makes the removal of unwanted code from the source file of a specific version a one-step process (against code traversal and manual removal of if statements).