Documentation | Get Started | Developer Chat
DeepSource helps you ship good quality code.
An ext-tensible loader to import anything like it is a python module.
Supports Python 3.6+.
pip install import-x
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.>>>fromimport_ximportExtensionLoader>>>classJsonLoader(ExtensionLoader):
extension='.json'auto_enable=False@staticmethoddefhandle_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()
>>>withjson_imports:
importfoo>>>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
fromimport_x.loaders.json_loaderimportJsonLoaderand you are ready to import all the json files.