Json schema Model
项目描述
使用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的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 9d753f6899d535c0e6db74865c11d59ec57af5f969e83120f38d94efebdcc832 |
|
| MD5 | 86ee6250b0ad46ff90864e8606f1f267 |
|
| BLAKE2b-256 | fc63e785630ef276ffacb4f9ecde7c9fdccf2e3843cf61b6caa72ae07583a92e |