4 ms·
So it seems you can register a source transformer with python which will trigger when a `# coding: ...` header is used (to signal character encoding). This see
by mjburgess 4y ago
So it seems you can register a source transformer with python which will trigger when a `# coding: ...` header is used (to signal character encoding).
This seems like an interesting avenue for static analysis tools, code generators, (etc.) to explore. Is it a viable approach?
I'd be interested in some analysis on this as a mechanism: performance, maintability, etc. wise.
The "dont do this" argument writes itself; nevertheless, its worth exploring.
- ktpsns 4y agoFunnily this reminds me of how racket defines a language (https://beautifulracket.com/explainer/lang-line.html https://beautifulracket.com/explainer/lang-line.html). Or how basically a shebang line does the same for any kind of script (https://en.wikipedia.org/wiki/Shebang_(Unix) https://en.wikipedia.org/wiki/Shebang_(Unix)).
- leethargo 4y agoTrue, but I think that source transformation on import are already supported in Python ("officially") based on importlib [0-1]. Here's an example use cases where the author is trying to create a Python enum from Thrift specs: [2]. The issue here is that the spec code is not necessarily valid Python, but can be fixed easily with some search & replace operations. [0] https://docs.python.org/3/reference/import.html https://docs.python.org/3/reference/import.html [1] https://docs.python.org/3/library/modules.html https://docs.python.org/3/library/modules.html [2] http://wakandan.github.io/2018/06/07/python3-import-hook.html http://wakandan.github.io/2018/06/07/python3-import-hook.htm...
- leethargo 4y agoOne of the benefits of implementing your own module finder and loader is that you could use a separate file extension to avoid confusion with actual Python code.
- est 4y agobehold, the .jsx but for python https://github.com/dropbox/pyxl https://github.com/dropbox/pyxl https://github.com/pyxl4/pyxl4 https://github.com/pyxl4/pyxl4