https://github.com/dequis/wild_hook
very useful python syntax extension
https://github.com/dequis/wild_hook
Last synced: 3 months ago
JSON representation
very useful python syntax extension
- Host: GitHub
- URL: https://github.com/dequis/wild_hook
- Owner: dequis
- Created: 2020-04-26T17:41:00.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-04-26T18:07:51.000Z (about 5 years ago)
- Last Synced: 2025-03-07T03:30:57.016Z (3 months ago)
- Language: Python
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Wild hook
A python import hook that rewrites source files to add one extra feature:
import module*
That's all.
It only imports the first match. Might break zip imports. Likely also disables
`.pyc` caching. Doesn't work with `__main__` so use `python -m`.## Installation
$ cp wild_hook.py usercustomize.py $(python -m site --user-site)
## Demo
$ cat example.py
import test*
$ cat testfoo.py
print('foo')
$ python -m example
fooExciting, right?
## Why
A certain someone read the docs of openscad and thought that includes behaved
like this, allowing wildcards but only taking the first match. A couple of
hours later we realized it doesn't, but I already had 80% of this shitpost
written up.## What name do I use to access that module in the code?
Does it look like I thought this through?
## References
These pages were useful while writing this:
* https://docs.python.org/3/library/importlib.html#setting-up-an-importer
* A bare but useful example on how to set up an importer that just uses the
same class as the default one.
* https://stackoverflow.com/questions/43571737/how-to-implement-an-import-hook-that-can-modify-the-source-code-on-the-fly-using
* Has a concrete example of subclassing those classes, although not in the
most convenient way.
* https://stackoverflow.com/questions/214881/can-you-add-new-statements-to-pythons-syntax
* Several neat ideas, like rewriting the token stream with the tokenizer
module (used in this project), or using sys.settrace (not used)
* https://github.com/ajalt/fuckitpy
* Didn't use any ideas from here, but worth looking at for AST manipulation
inspiration