Package: pypreprocessor
Featuressupport c-style directives in pythonusing an intentionally limited subset of the c directives, and then
some…can run post-processed code on-the-flypre-process and run code all in one shotaccurately preserves error tracebacks for the source filecan output to a fileas easily as setting a flagwith a user specified filename if definedcan strip all pre-processor data from the outputas easily as setting a flagremoves all preprocessor directivesremoves all preprocessor specific code#defines can be set in code prior to processinguseful if you need to run a file in different modeseliminates the need for decision logic in the preprocessor itselfnested #ifdef directives are supportedhelpfull for more complicated codeendifall gives the opportunity to end all open blocksBenefitsEase of Useall of the functionality required to run the pypreprocessor is
contained in a single moduleadding preprocessor support is as simple as importing pypreprocessor
to a file containing preprocessor directives and telling it to runthe preprocessor preserves errors in the pre and post processed code
as much as possiblethe preprocessor will break execution and output traceback errors if
it sees invalid preprocessor directivesDynamicpypreprocessor has multiple option of operationit can generate a new post-processed version of a source file absent
all of the preprocessor informationor it can pre-process and run code transparently the same way as
c-style languages doSimplethe source footprint for the pypreprocessor code is very smallthe preprocessor is designed to be as simple and lightweight as
possibleSyntaxThe syntax for pypreprocessor uses a select subset of the stanard
c-style preprocessor directives, and then some…Supported directivesdefine non-value constants used by the preprocessor#define constantremove a non-value constant from the list of defined constants#undef constantmakes the subsequent block of code available if the specified
constant is set#ifdef constantmakes the subsequent block of code available if all of the preceding
#ifdef statements return false#elserequired to close out an #ifdef/#else block#endifpossibility to close all open blocks#endifallexclude the subsequent block of code (conditionals not included). I
know it doesn’t fit into the standard set of c-style directives but
it’s too handy to exclude (no pun).#excluderequired to close out an #exclude block#endexcludeOptionsThe following options need to be set prior to pypreprocessor.parse()pypreprocessor.defines.append('define')add defines to the preprocessor programmatically, this allows the source
file to have some decision logic to decide which ‘defines’ need to be
setpypreprocessor.run=True/Falsepypreprocessor.resume=True/Falsepypreprocessor.save=True/Falseset the options of the preprocessor:run: Run the preprocessed code if true. Default is trueresume: Return after a file is preprocessed and can preprocess a next
file if true. Default is falsesave: Save preprocessed code if true. Default is truepypreprocessor.input='inputFile.py'required if you are preprocessing a module that is going to be imported.
you can also use it to process external files.pypreprocessor.output='outputFile.py'set this to get a user defined name for the output file, otherwise the
default is used:'inputFile_out.py'pypreprocessor.removeMeta=Trueset this to remove the metadata from the output, useful if you’re
generating a ‘clean’ version of the sourcepypreprocessor.readEncoding=sys.stdin.encodingpypreprocessor.writeEncoding=sys.stdout.encodingset this to make preprocessor use encodingApplicationsInclude support for debug-specific informationIt is often useful to provide statements within code specific to
debugging, such as, print statements used to verify correct outputs.
But, removing those debug statements can be painful and introduce new
bugs while migrating to the production version.Why not leave them…By setting a “#define debug” at the top of the module and wrapping all
of the debug statements in “#ifdef debug” blocks; disabling that code
during production is as easy as adding another hash mark before the
“#define debug” statement.For a working example see:/examples/debug.pyPython2 -> Python3 code conversionProducing code that can be run in both Python 2 and Python 3 is as easy
as…check the version of the python interpreter in useset a #define in the preprocessor indicating the versionadd #ifdef directives on the code that is version specificFor a working example see:/examples/py2and3.pyWriting code from Debug -> Production in the same fileEver wanted to run a file with debug-specific code during development
but output a clean version specific source file for release? Here’s
how…make a source file that takes the arguments ‘debug’ and ‘production’put the debug code in “#ifdef debug” blocksthen run ‘filename.py debug’ during developmentor ‘filename.py production’ to output a version without any of the
pypreprocessor metadataFor a working example see:/examples/debug2production.pyNote: This file also contains a ‘postprocessed’ mode that shows what
pypreprocessor actually outputs. It’s so simple it’ll probably make you
::facepalm:: ;)