Import non-python files
Project description
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:
{
"why": "not",
}
# 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
from import_x.loaders.json_loader import JsonLoader
and you are ready to import all the json files.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file import-x-0.1.1.tar.gz.
File metadata
- Download URL: import-x-0.1.1.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.17 CPython/3.7.4 Linux/5.2.14-arch1-1-ARCH
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9b9b603b280f553eb075a01f877b9f1afa1de045b41d10508b667c2311324e2d
|
|
| MD5 |
ef39dafdc93b1b93a23041bf95a059c3
|
|
| BLAKE2b-256 |
aab38322b911337b4ad4a8c31a6679afcb9050b1086e1bbab4163525fec51c5f
|
File details
Details for the file import_x-0.1.1-py3-none-any.whl.
File metadata
- Download URL: import_x-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.17 CPython/3.7.4 Linux/5.2.14-arch1-1-ARCH
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cfd7c9503b0aeb95d23fb80193964b7874fd5c2bf24f136a4a92f4c03b391990
|
|
| MD5 |
6f2056dd73bf7ddab6e7c6f481de739c
|
|
| BLAKE2b-256 |
06264535a64bb032bab4866e1d08be609152736e05691c2f852d0ca5d17944e0
|