Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/deepsourcecorp/import-x
Import anything like it is a python module
https://github.com/deepsourcecorp/import-x
import import-hook json python
Last synced: 2 months ago
JSON representation
Import anything like it is a python module
- Host: GitHub
- URL: https://github.com/deepsourcecorp/import-x
- Owner: DeepSourceCorp
- License: mit
- Created: 2019-04-29T15:15:29.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-11-08T08:00:01.000Z (about 3 years ago)
- Last Synced: 2024-10-11T14:46:11.681Z (3 months ago)
- Topics: import, import-hook, json, python
- Language: Python
- Homepage:
- Size: 10.7 KB
- Stars: 39
- Watchers: 6
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Documentation |
Get Started |
Developer Chat
DeepSource helps you ship good quality code.---
# import-x
An ext-tensible loader to import anything like it is a python module.
Supports Python **3.6+**.
## Installation
```
pip install import-x
```## Usage
Example json file in your path ``foo.json``:
```json
{
"why": "not",
}
``````python
# Extend the ExtensionLoader and implement 'handle_module' method
# where you will get a module object and the path to that module.>>> from import_x import ExtensionLoader
>>> class JsonLoader(ExtensionLoader):
extension = '.json'auto_enable = False
@staticmethod
def handle_module(module, path):
"""
Load the json file and set as `data` attribute of the module.
"""
json_file = Path(path)
content = json_file.read_text()
try:
data = json.loads(content)
except (json.JSONDecodeError, ValueError):
data = {}
module.data = data>>> json_imports = JsonLoader()
>>> with json_imports:
import foo
>>> foo.data
>>> {"why": "not"}
```If you want to enable imports automatically without the context_manager then just
do ``auto_enable = True`` in your loader.This Example ``JsonLoader`` can be used directly by importing
```python
from import_x.loaders.json_loader import JsonLoader
```and you are ready to import all the json files.