提供给定json模式随机样本
项目描述
freddy
提供符合给定模式的随机化json数据(样本)。
适用于json模式和pydantic模型。
使用方法
pydantic
import datetime
from pprint import pprint
from typing import List, Optional
from pydantic import BaseModel, Field
import freddy
class User(BaseModel):
id: int
name = 'John Doe'
signup_ts: Optional[datetime.datetime] = None
friends: List[int] = []
pattern_field: str = Field(..., regex=r"^[-_a-zA-Z0-9]+$")
sample = freddy.sample(User)
pprint(sample)
{'id': 452, 'signup_ts': '1903-03-12T20:20:00', 'friends': [675, 408], 'pattern_field': 'EUvKs7BIK-Ne', 'name': 'xfphlync'}
User.validate(sample)
User(id=452, signup_ts=datetime.datetime(1903, 3, 12, 20, 20), friends=[675, 408], pattern_field='EUvKs7BIK-Ne', name='xfphlync')
jsonschema
from pprint import pprint
import jsonschema
import freddy
family_schema = {
"type": "array",
"items": {
"properties": {
"member": {"$ref": "#/definitions/person"},
"role": {"$ref": "#/definitions/role"},
},
"type": "object",
},
"maxItems": 5,
"minItems": 1,
"definitions": {
"person": {
"properties": {
"age": {"type": "integer"},
"name": {"type": "string"},
"pets": {
"items": {"$ref": "#/definitions/pet"},
"maxItems": 2,
"type": "array",
},
},
"type": "object",
},
"pet": {
"properties": {
"kind": {"enum": ["dog", "cat"], "type": "string"},
"name": {"type": "string"},
},
"type": "object",
},
"role": {
"enum": [
"father",
"mather",
"son",
"daughter",
"aunt",
"grandma",
"grandpa",
],
"type": "string",
},
}
}
# Get 10 random samples
for i in range(10):
sample_family = freddy.sample(family_schema)
# Validate against schema
jsonschema.validate(sample_family, family_schema)
pprint(sample_family)
[
{"member": {"age": 77, "name": "k", "pets": []}, "role": "grandma"},
{"member": {"age": 64, "name": "naifvxf", "pets": []}, "role": "grandpa"},
{
"member": {
"age": 23,
"name": "itruydotrj",
"pets": [{"kind": "cat", "name": "o"}, {"kind": "cat", "name": "uonmvfgd"}],
},
"role": "son",
},
]
安装
pip install freddy
开发
# Clone the repo
git@github.com:lferran/freddy.git
cd freddy
make develop
# Run tests
make tests
JSON Schema支持
符合JSON Schema Draft 7。以下特性被支持
-
布尔类型
-
null类型
-
字符串类型
-
数字类型
-
整数类型
-
数组类型
-
对象类型
-
定义/引用
-
布尔类型
-
常量
-
exclusiveMinimum
和exclusiveMaximum
在整数和数字中。 -
数字
multipleOf
关键字 -
字符串
pattern
正则表达式关键字 -
required
关键字 -
additionalProperties
-
所有内置字符串格式
-
能够提供自定义基本类型工厂
-
多类型:
{"type": ["string", "array"]}
-
查看
allOf
:生成多个对象+合并
不支持
- ID引用
allOf
和not
关键字- 条件关键字
if
,then
和else
- 对象上的
patternProperties
- 对象上的属性和模式
dependencies
项目详情
关闭
freddy-3.1.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 37f6e075efede218a6b1870589d39eadb930a88fc908e3ecbdd47be73b0f7eb5 |
|
MD5 | 25bdab09ea95fa12f6b59dfe5b220534 |
|
BLAKE2b-256 | 831efe1299025001761a1cd0c29ed8fff15a4ca98f332ee6fa8fc372a4dfb9f9 |