像使用python模块一样使用json文件
json-sempai的Python项目详细描述
你有没有在夜里睡不着觉,绝望地渴望 只需直接导入json文件,就好像它们是python模块一样 [1]?现在你可以了!
这种可憎让你可以写作
importsome_json_file
如果可以找到some_json_file.json,它就可以像 python模块。
用法
在python路径上的某个地方插入一个json文件。tester.json:
{"hello":"world","this":{"can":{"be":"nested"}}}
现在导入jsonsempai和json文件
>>>fromjsonsempaiimportmagic>>>importtester>>>tester<module'tester'from'tester.json'>>>>tester.hellou'world'>>>tester.this.can.beu'nested'>>>
或者,可以使用上下文管理器(100%减少魔力):
>>>importjsonsempai>>>withjsonsempai.imports():...importtester>>>tester<module'tester'from'tester.json'>
还支持python包:
$ tree . └── python_package ├── file.json ├── __init__.py └── nested_package ├── __init__.py └── second.json
>>>fromjsonsempaiimportmagic>>>frompython_packageimportfile>>>file<module'python_package.file'from'python_package/file.json'>>>>importpython_package.nested_package.second>>>python_package.nested_package.second<module'python_package.nested_package.second'from'python_package/nested_package/second.json'>
安装
从pip安装:
$ pip install json-sempai
或者克隆此repo并从源安装:
$ python setup.py install
从您的机器中清除此恐怖:
$ pip uninstall json-sempai
[1] | Disclaimer: Only do this if you hate yourself and the rest of the world. |