跳转到主要内容

将JSON文件用作Python模块

项目描述

https://travis-ci.org/kragniz/json-sempai.png?branch=master https://pypip.in/version/json-sempai/badge.png?style=flat

你是否曾在夜晚辗转反侧,迫切地想要做的只有直接导入JSON文件,就像它们是Python模块一样?现在你可以了!

这个令人厌恶的功能允许你编写

import some_json_file

如果找到了some_json_file.json,它将像Python模块一样可用。

用法

将json文件放在你的Python路径的某个位置。 tester.json

{
    "hello": "world",
    "this": {
        "can": {
            "be": "nested"
        }
    }
}

现在导入jsonsempai和你的json文件!

>>> from jsonsempai import magic
>>> import tester
>>> tester
<module 'tester' from 'tester.json'>
>>> tester.hello
u'world'
>>> tester.this.can.be
u'nested'
>>>

或者,可以使用上下文管理器(100%无魔法)

>>> import jsonsempai
>>> with jsonsempai.imports():
...     import tester
>>> tester
<module 'tester' from 'tester.json'>

也支持Python包

$ tree
.
└── python_package
    ├── file.json
    ├── __init__.py
    └── nested_package
        ├── __init__.py
        └── second.json
>>> from jsonsempai import magic
>>> from python_package import file
>>> file
<module 'python_package.file' from 'python_package/file.json'>
>>> import python_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

或克隆此存储库并从源安装

$ python setup.py install

要从你的机器上清除这个恐怖

$ pip uninstall json-sempai

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分发

json-sempai-0.4.0.tar.gz (3.9 kB 查看哈希值)

上传时间

构建分发版

json_sempai-0.4.0-py2.py3-none-any.whl (6.2 kB 查看哈希值)

上传时间 Python 2 Python 3

支持