PreProcess Compiler Directives

The first version of ‘PreProcess Compiler Directives’ is now, finally, uploaded to PyPI - CompilerDirectives

Have posted in this new thread for discussion and suggested improvements as advised in the earlier thread discussing this: [Modify 'import' command - #84 by Rosuav].

Would appreciate anyone willing to test this out to check that it is generally working. There may be some edge cases which are not yet catered for so let me know if you find something which doesn’t work.

@Rosuav have included a #COMP directive for ‘compile-time’ evaluation further to your message in the earlier thread.

Your README is frankly unreadable. Anyone without knowledge of the prior context would not be able to understand it. The code snippets are also misformatted. I suggest you add a summary of what your library does.

Sorry about that I thought the readme.md looked ok in PyCharm but something is obviously wrong with it. Let me see if I can put the docs here for now until I fix that.

**[u]Pre-process compiler directive[/u]**

Normal usage:

    import CompilerDirectives
    import Constants             # file containing our constant definitions

Diagnostics:

    For diagnostic purposes an intermediate modified file can be saved in the \__pycache_\_ directory by setting the diagnostic variable:

    DIAG_PY2_SAVE = True

    The writing of this file is automatically disabled when Python is run with \__debug_\_ False.
    Setting this to True also enables display of speed testing in the Python Console.

    This can be either done before the import:

        DIAG_PY2_SAVE = True
        import CompilerDirectives

    Or after:

        import CompilerDirectives
        CompilerDirectives.PreProcess.DIAG_PY2_SAVE = True

Compiler directives:

    Define constant
        #DEFINE / #DEF <const> <value>

    Undefine constant
        #UNDEF <const>

    Compile time conditionals
        #IF <expr> / #ELIF <expr> / #ENDIF

    Compile time definition conditionals
        #IFDEF <expr> / #IFNDEF <expr> / #ENDIF

    Include file
        #INCLUDE <file>

    ‘Compile’ time expression evaluation, remove ‘#COMP’ from resulting line
        #COMP <expr>                                                

     Multiple items can be included on the same line delimited by ‘,’ (comma):
        #DEF <const1> <value1>, <const2> <value2>
        #UNDEF <const1>, <const2>
        #INCLUDE <file1>, <file2>

Constant replacement:

    After CompilerDirectives is imported all further imports will be subject to replacement of their Constants with their respective values if previously defined.

    All files imported can contain further constant definitions and un-definitions which will be added to / removed from the application level ‘global’ Constants list.

    Constant replacement will happen only once for each import with the resulting byte-code being saved in the \*.pyc \__pycache_\_ file.  When a modified file is imported which contains any compiler directives all \*.pyc files in the \__pycache_\_ directory older than that imported file will be deleted so as to force their re-compilation the next time they are accessed.

Development:

    For development purposes the following can be used to remove all \*.pyc files on each run so that these get recreated and therefore Pre-Process runs every time:

        import CompilerDirectives
        CompilerDirectives.PreProcess.stale_pyc_del(\__file_\_)

    Alternatively to remove all earlier \*.pyc files on each run so that Pre-Process runs every time regardless of whether the test module is modified or not pass just the directory.:

        import os
        import CompilerDirectives
        CompilerDirectives.PreProcess.stale_pyc_del(os.path.dirname(\__file_\_))

Known issue:

        There is a known issue with tokenize.tokenize not indenting & dedenting correctly when a #CompilerDirective (comment) is the first line after aforementioned indentation change.

Neat idea! What’s the main use case where this beats if __debug__: / python -O or ordinary runtime config checks?

Just curious your motivation - Performance, or something those approaches can’t handle?

Readme.md file now correctly formatted and uploaded to PyPI. Sorry about that.

@ssweber various reasons; it just felt wrong to me that Python didn’t have proper compile-time directives and genuine constant replacement.

Performance is part of it; once ‘compiled’ into a .pyc constants don’t keep having to be resolved on each run. Also the ability to #INCLUDE text from another file. Also #COMP ‘compile’-time evaluation… and any other benefits which might come to light if this gets wide-scale use and acceptance.

I think the docs are missing complete (toy) examples. Knowing some C/C++, I can make a guess on what the ‘compiler directives’ do, but those assumptions may be incorrect. I suggest writing the documentation from the perspective of an average Python developer, and putting the minimum information to get started at the top (and all the advanced debugging stuff on the bottom).

An AI may be helpful in this exercise.

@inventshah Examples are in compilerdirectives\tests\ with the main file being: Test.py and the imported files: Constants.py, Moduile1.py & Moduie2.py

However at the moment I can’t see how I am supposed to make them easy to access from PyPI.

Perhaps sharing/making your repository public could be a way to do it. I don’t know if I’m the only one, but I rarely try code on pypi without first having al look at the (usually) public repository. I realize that someone could upload code to pypi that is different than that available in the public repository…

@aroberge Not sure if I have understood correctly but are you suggesting I setup some sort of public repository for hosting the example code or is there a way within PyPI for doing it that I am missing.

Most projects that I see on pypi have a public repository on Github or Gitlab. For example, if you look at my token-utils project on pypi, you will see a link (on the right-hand side, under “Projects links”) to the repository.

You could also duplicate some of the code(snippets) into the readme.

New version now uploaded with the examples/test code built-in to the Readme.md, I hadn’t realised that I could put code snippets in there.