跳转到主要内容

Json schema Model

项目描述

Build status Downloads Latest Version License

使用JSON模式构建Python对象

>>> import jsonschema_model
>>> Model = jsonschema_model.model_factory({
...    "name": "Model",
...    "properties": {
...        "foo": {"type": "string"},
...        "bar": {"type": "array", "items": {
...            "type": "object",
...            "name": "Bar",
...            "properties": {
...                "zaz": {"type": "string"},
...            },
...        }},
...    }})

# Simple object creation
>>> obj = Model(foo="bar")
>>> assert obj == {"foo": "bar"}

# Nested and array are implemented
# HINT: Use add() instead of append()
>>> obj.bar.add(zaz="qux")
{'zaz': 'qux'}
>>> assert obj == {'foo': 'bar', 'bar': [{'zaz': 'qux'}]}

# You can access via attribute or via dict like interface
>>> obj["bar"][0].zaz
'qux'

# Array have a special get_or_create() method
# to avoid dupplicates within an array
>>> obj.bar.get_or_create(zaz="xuq")
{'zaz': 'xuq'}
>>> obj.bar
[{'zaz': 'qux'}, {'zaz': 'xuq'}]
>>> obj.bar.get_or_create(zaz="xuq")
{'zaz': 'xuq'}
>>> obj.bar
[{'zaz': 'qux'}, {'zaz': 'xuq'}]

项目详情


下载文件

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

源分发

jsonschema-model-1.4.0.tar.gz (4.8 kB 查看哈希值)

上传时间:

构建分发

jsonschema_model-1.4.0-py2.py3-none-any.whl (4.3 kB 查看哈希值)

上传时间: Python 2 Python 3

支持