install
$ easy_install tinyschema
demo
from tinyschema.schema import Schema
from tinyschema import fields
class Person(object):
def __init__(self, name=None, age=None):
self.name = name
self.age = age
class PersonSchema(Schema):
name = fields.string()
age = fields.integer()
class SuperPersonSchema(PersonSchema):
skill = fields.noduplicated()
class ParentsSchema(Schema):
mother = PersonSchema(after_parse= lambda kwargs : Person(**kwargs))
father = PersonSchema(after_parse= lambda kwargs : Person(**kwargs))
print PersonSchema.parse({"name": "foo", "age": "10"})
print SuperPersonSchema.parse({"name": "foo", "age": "10", "skill": ["foo", "bar", "baz", "foo", "bar"]})
params = {"mother": {"name": "foo", "age": "30"},
"father": {"name": "bar", "age": "30"},
}
print ParentsSchema.parse(params)
追記
from tinyschema.schema import ObjectSchema
class PersonSchema(ObjectSchema):
name = fields.string()
age = fields.integer()
print PersonSchema.parse({"name": "foo", "age": "10"})
print PersonSchema.parse({"name": "foo", "age": "10"}).name
print PersonSchema.parse({"name": "foo", "age": "10"}).age
from tinyschema import schema
ParentsSchema = schema.schema_factory("ParentsSchema", ObjectSchema,
mother=PersonSchema(), father=PersonSchema())
print ParentsSchema.parse(params)