Schematics
nome* | idade | linguagens |
---|---|---|
jess | 42 | [Python, Go, R] |
jairo | 102 | [Python, Go, JavaScript] |
Validar dados não tabulares
{
"nome": "Jess",
"idade": 42,
"linguagens": [
"Python",
"Go",
"R"
]
}
from schematics.models import Model
from schematics.types import StringType, ListType, IntType
class Pessoa(Model):
nome = StringType(required=True)
idade = IntType()
linguagens = ListType(StringType)
jess_data = {
"nome": "Jess",
"idade": 42,
"linguagens": [
"Python",
"Go",
"R"
]
}
jess = Pessoa(jess_data)
obj.validate()
pessoa/
├── __init__.py
└── models.py
from pessoa.models import Pessoa
jess_data = {
"nome": "Jess",
"idade": 42,
"linguagens": [
"Python",
"Go",
"R"
]
}
jess = Pessoa(jess_data)
jess.validate() == None
obj.to_native()
from pessoa.models import Pessoa
jess_data = {
"nome": "Jess",
}
jess = Pessoa(jess_data)
jess.to_native()
from schematics.models import Model
from schematics.types import StringType, ListType, IntType
class Pessoa(Model):
nome = StringType(required=True)
idade = IntType(serialize_when_none=False)
linguagens = ListType(StringType,
serialize_when_none=False)
from pessoa.models import Pessoa
jess_data = {
"nome": "Jess",
}
jess = Pessoa(jess_data)
jess.to_native()
from schematics.models import Model
from schematics.types import StringType, ListType, ModelType, IntType
class Pessoa(Model):
nome = StringType(required=True,
serialize_when_none=False,
deserialize_from="name")
idade = IntType(serialize_when_none=False,
deserialize_from="age")
linguagens = ListType(StringType,
serialize_when_none=False,
deserialize_from="lang")
from pessoa.models import Pessoa
jess_data = {
"name": "Jess",
"age": "42",
"lang": [
"Python",
"Go",
"R"
]
}
jess = Pessoa(jess_data)
jess.validate() == None
jess.to_native()
from schematics.models import Model
from schematics.types import StringType, ListType, ModelType, IntType
class Pessoa(Model):
nome = StringType(required=True,
serialize_when_none=False,
deserialize_from="name",
serialized_name="name")
idade = IntType(serialize_when_none=False,
deserialize_from="age",
serialized_name="age")
linguagens = ListType(StringType,
serialize_when_none=False,
deserialize_from="lang",
serialized_name="blabla")
from pessoa.models import Pessoa
jess_data = {
"name": "Jess",
"age": "42",
"lang": [
"Python",
"Go",
"R"
]
}
jess = Pessoa(jess_data)
jess.validate() == None
jess.to_native()
Fields com classe
{
"name": "Jess",
"age": "42",
"lang": [
"Python",
"Go",
"R"
],
"pets": [{
"name": "Channel",
"fur": "branca"
},
{
"name": "Cora",
"fur": "preta"
}
]
}
from schematics.models import Model
from schematics.types import StringType, ListType, ModelType, IntType
class PetType(Model):
nome = StringType(required=True,
deserialize_from="name",
serialized_name="name")
cor = StringType(required=True,
deserialize_from="fur",
serialized_name="fur")
class Pessoa(Model):
nome = StringType(required=True,
deserialize_from="name",
serialized_name="name")
idade = IntType(serialize_when_none=False,
deserialize_from="age",
serialized_name="age")
linguagens = ListType(StringType,
serialize_when_none=False,
deserialize_from="lang",
serialized_name="blabla")
pets = ListType(ModelType(PetType))
from pessoa.models import Pessoa
jess_data = {
"name": "Jess",
"age": "42",
"lang": [
"Python",
"Go",
"R"
],
"pets": [
{"name": "cora", "fur": "preta"},
{"name": "channel", "fur": "branca"}
]
}
jess = Pessoa(jess_data)
jess.validate() == None
@jesstemporal
@pizzadedados
Schematics: Modelando dados de um JSON
By Jessica Temporal
Schematics: Modelando dados de um JSON
Usando a biblioteca Python Schematics para validar dados de um JSON.
- 636