schemaconvertor - 转换对象工具


BSD
Windows
Python

软件简介

schemaconvertor
提供了一种使用schema来转换对象的方法,通过schema,可以指定该对象序列化的部分和对应的类型,其结果可以进一步序列化为json。

演示

假设有个简单的数据类型User:

from collections import namedtuple

User = namedtuple("User", ["name", "password", "age"])

可以通过指定schema来转换对象:

schema = {
    "type": "object",
    "properties": {
        "name": {
            "type": "string"
        },
        "age": {
            "type": "integer"
        }
    }
}

user = User(name="lyc", password="schemaconvertor", age="24")

from schemaconvertor.convertor import convert_by_schema

print convert_by_schema(user, schema)

输出:

{‘age’: 24, ‘name’: ‘lyc’}

更多示例:demo
0.3