How would you execute the equivalent of preprocessor directives in Python?

Is there a way to make the following preprocessor directives in Python?

#if DEBUG < do some code > #else < do some other code > #endif 
+54
python preprocessor equivalent directive
Jan 27 '09 at 1:10
source share
7 answers

Here is __debug__ , which is a special value that the compiler executes with the preprocess.

 if __debug__: print "If this prints, you're not running python -O." else: print "If this prints, you are running python -O!" 

__debug__ will be replaced with a constant 0 or 1 by the compiler, and the optimizer will delete any lines if 0: before your source is interpreted.

+98
Jan 27 '09 at 3:25
source share

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() #define debug #ifdef debug print('The source is in debug mode') #else print('The source is not in debug mode') #endif 

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

+32
Jun 07 2018-10-06T00
source share

I suspect you hate this answer. How do you do it in Python,

 # code here if DEBUG: #debugging code goes here else: # other code here. 

Since python is an interpreter, there is no preprocessing step, and there is no particular advantage to having special syntax.

+22
Jan 27 '09 at 1:14
source share

You can use a preprocessor in Python. Just run your scripts via cpp (C-Preprocessor) in the bin directory. However, I did this with Lua, and the benefits of a simple interpretation outweighed the more complex IMHO compilation.

+10
Jan 27 '09 at 1:16
source share

You can simply use the plain language construct:

 DEBUG = True if DEBUG: # Define a function, a class or do some crazy stuff def f(): return 23 else: def f(): return 42 
+3
Jan 27 '09 at 1:14
source share

An alternative method is to use a bash script to comment on parts of the code that are relevant only to debugging. The following is an example script that describes the lines that contain the #DEBUG statement. It can also delete these comment markers again.

 if [ "$1" == "off" ]; then sed -e '/^#/! {/#DEBUG/ s/^/#/}' -i *.py echo "Debug mode to $1" elif [ "$1" == "on" ]; then sed -e '/#DEBUG/ s/^#//' -i *.py echo "Debug mode to $1" else echo "usage: $0 on | off" fi 
+2
Jul 07 '15 at 6:47
source share
  • Python if cannot remove elements from arrays.
  • C precompilers do not handle #! or other lines starting with C # as needed.
  • pypreprocessor seems to be python specific

Instead, use regular m4, like this:

 ifelse(DEBUG,True,dnl' < do some code > dnl,dnl' < do some other code >dnl ') ifelse( M4_CPU,x86_64,' < do some code specific for M4_CPU > ',M4_CPU,arm,' < do some code specific for M4_CPU > ',M4_CPU,ppc64le,' < do some code specific for M4_CPU > ') ifelse( M4_OS,windows,' < do some code specific for M4_OS > ',M4_OS,linux,' < do some code specific for M4_OS > ',M4_OS,android,' < do some code specific for M4_OS > ') 

m4 -D DEBUG = True -D M4_OS = android -D M4_CPU = arm test.py.m4> test.py

0
Jan 22 '19 at 16:30
source share



All Articles