跳转到主要内容

提供给定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类型

  • 字符串类型

  • 数字类型

  • 整数类型

  • 数组类型

  • 对象类型

  • 定义/引用

  • 布尔类型

  • 常量

  • exclusiveMinimumexclusiveMaximum 在整数和数字中。

  • 数字 multipleOf 关键字

  • 字符串 pattern 正则表达式关键字

  • required 关键字

  • additionalProperties

  • 所有内置字符串格式

  • 能够提供自定义基本类型工厂

  • 多类型:{"type": ["string", "array"]}

  • 查看 allOf:生成多个对象+合并

不支持

  • ID引用
  • allOfnot 关键字
  • 条件关键字 ifthenelse
  • 对象上的 patternProperties
  • 对象上的属性和模式 dependencies

项目详情


下载文件

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

源分布

freddy-3.1.0.tar.gz (6.9 kB 查看哈希值)

上传时间:

由以下支持