Created
October 28, 2016 08:29
-
-
Save tsauerwein/93c88ba3c37f936dea113a27447c0db5 to your computer and use it in GitHub Desktop.
Colander and None?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import colander | |
class NoneSequence(colander.MappingSchema): | |
foo = colander.SchemaNode(colander.String()) | |
bar = colander.SchemaNode( | |
colander.Sequence(), colander.SchemaNode(colander.String()), | |
missing=None) | |
class NestedNoneSequence(colander.MappingSchema): | |
a = colander.SchemaNode(colander.String()) | |
b = NoneSequence() | |
schema = NestedNoneSequence() | |
deserialized = schema.deserialize({"a": "a", "b": {"foo": "abc", "bar": None}}) | |
# colander.Invalid: {'b.bar': '"None" is not iterable'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import colander | |
class Tuple(colander.TupleSchema): | |
val1 = colander.SchemaNode(colander.String()) | |
val2 = colander.SchemaNode(colander.Int()) | |
class TupleSchema(colander.MappingSchema): | |
t = Tuple(missing=None) | |
class NestedTupleSchema(colander.MappingSchema): | |
n = TupleSchema() | |
schema = NestedTupleSchema() | |
print(schema.deserialize({"n": {"t": ["s", 1]}})) | |
# {'b': {'a': {'val': 'ok'}}} | |
print(schema.deserialize({"n": {"t": None}})) | |
# colander.Invalid: {'n.t': '"None" is not iterable'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import colander | |
class SchemaA(colander.MappingSchema): | |
val = colander.SchemaNode(colander.String()) | |
class SchemaB(colander.MappingSchema): | |
a = SchemaA(missing=None) | |
class BodySchemaC(colander.MappingSchema): | |
b = SchemaB() | |
schema = BodySchemaC() | |
print(schema.deserialize({"b": {"a": {"val": "ok"}}})) | |
# {'b': {'a': {'val': 'ok'}}} | |
print(schema.deserialize({"b": {"a": None}})) | |
# colander.Invalid: {'b.a': '"None" is not a mapping type: Does not implement dict-like functionality.'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import colander | |
class NoneSequence(colander.MappingSchema): | |
foo = colander.SchemaNode(colander.String()) | |
bar = colander.SchemaNode( | |
colander.Sequence(), colander.SchemaNode(colander.String()), | |
missing=None) | |
schema = NoneSequence() | |
deserialized = schema.deserialize({'foo': '1', 'bar': None}) | |
# colander.Invalid: {'bar': '"None" is not iterable'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import colander | |
class Person(colander.MappingSchema): | |
name = colander.SchemaNode(colander.String(), missing=None) | |
schema = Person() | |
deserialized = schema.deserialize({'name': None}) | |
print(deserialized) | |
# {'name': None} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment