Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



Funnily this reminds me of how racket defines a language (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)).


True, 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 [1] https://docs.python.org/3/library/modules.html [2] http://wakandan.github.io/2018/06/07/python3-import-hook.htm...


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





Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: