导入非python文件

import-x的Python项目详细描述


一个ext-tensible加载程序,可以导入类似于python模块的任何内容。

支持python3.6+

用法

路径中的示例json文件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"}

如果要在没有上下文管理器的情况下自动启用导入,则只需 在加载程序中执行auto_enable = True

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Java Spring REST API JSON在postman中显示,但在测试中未“看到”   java如何以编程方式检查旋转设备?   java力穷举开关   amazon web services DynamoDB从Java更新列表失败   java Android studio IDE更改波斯注释字体   java如何将postgresql添加到OpenShift Spring应用程序?   java RecycleView分页是否在末尾添加新项?   如何通过java获取移动设备信息?   java为什么Apache Commons StringEscapeUtils escapeXML10不能在groovy中工作?   函数式编程java curry现有的静态函数   java与JList的合作