https://github.com/bswck/injection
Create Python variables on the fly.
https://github.com/bswck/injection
lazy-importing lazy-imports pep-690 python
Last synced: 9 days ago
JSON representation
Create Python variables on the fly.
- Host: GitHub
- URL: https://github.com/bswck/injection
- Owner: bswck
- License: mit
- Created: 2024-01-11T02:32:51.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-29T02:51:14.000Z (6 months ago)
- Last Synced: 2025-04-01T11:53:21.931Z (3 months ago)
- Topics: lazy-importing, lazy-imports, pep-690, python
- Language: Python
- Homepage:
- Size: 570 KB
- Stars: 89
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# injection
## How does that work?
_injection_ makes it possible to hook into variable name lookup via inserting a special key into scope dictionaries.### Example
```py
from functools import partial
from random import randintfrom injection import inject
roll: int
inject("roll", into=locals(), factory=partial(randint, 1, 6))print(roll, type(roll) is int) # 6 True
print(roll, type(roll) is int) # 4 True
print(roll, type(roll) is int) # 3 True# you never know what the value of roll will be!
```It could be used for various purposes, for instance as a robust replacement for Flask's [local proxies](https://flask.palletsprojects.com/en/stable/reqcontext/#notes-on-proxies) or to implement pure-Python PEP 690
that would import things on first reference.# Legal Info
© Copyright by Bartosz Sławecki ([@bswck](https://github.com/bswck)).
This software is licensed under the terms of [MIT License](https://github.com/bswck/injection/blob/HEAD/LICENSE).