Fix missing quotes and make spacing more standard

This commit is contained in:
Éric Araujo
2016-06-29 16:48:40 -04:00
committed by GitHub
parent 263698173f
commit 70c3cd369a

View File

@@ -19,11 +19,11 @@ serialization might look something like this:
:linenos: :linenos:
{ {
'name':'keith', 'name': 'keith',
'age':'20', 'age': '20',
'friends':[('1', 'jim'),('2', 'bob'), ('3', 'joe'), ('4', 'fred')], 'friends': [('1', 'jim'), ('2', 'bob'), ('3', 'joe'), ('4', 'fred')],
'phones':[{'location':'home', 'number':'555-1212'}, 'phones': [{'location': 'home', 'number': '555-1212'},
{'location':'work', 'number':'555-8989'},], {'location': 'work', 'number': '555-8989'}],
} }
Let's further imagine you'd like to make sure, on demand, that a Let's further imagine you'd like to make sure, on demand, that a
@@ -412,11 +412,11 @@ Deserializing A Valid Serialization
:linenos: :linenos:
cstruct = { cstruct = {
'name':'keith', 'name': 'keith',
'age':'20', 'age': '20',
'friends':[('1', 'jim'),('2', 'bob'), ('3', 'joe'), ('4', 'fred')], 'friends': [('1', 'jim'), ('2', 'bob'), ('3', 'joe'), ('4', 'fred')],
'phones':[{'location':'home', 'number':'555-1212'}, 'phones': [{'location': 'home', 'number': '555-1212'},
{'location':'work', 'number':'555-8989'},], {'location': 'work', 'number': '555-8989'}],
} }
schema = Person() schema = Person()
deserialized = schema.deserialize(cstruct) deserialized = schema.deserialize(cstruct)
@@ -429,11 +429,11 @@ conforms to the schema, ``deserialized`` will be the following:
:linenos: :linenos:
{ {
'name':'keith', 'name': 'keith',
'age':20, 'age': 20,
'friends':[(1, 'jim'),(2, 'bob'), (3, 'joe'), (4, 'fred')], 'friends': [(1, 'jim'), (2, 'bob'), (3, 'joe'), (4, 'fred')],
'phones':[{'location':'home', 'number':'555-1212'}, 'phones': [{'location': 'home', 'number': '555-1212'},
{'location':'work', 'number':'555-8989'},], {'location': 'work', 'number': '555-8989'}],
} }
Note that all the friend rankings have been converted to integers, Note that all the friend rankings have been converted to integers,
@@ -455,11 +455,11 @@ validation error?
import colander import colander
cstruct = { cstruct = {
'name':'keith', 'name': 'keith',
'age':'-1', 'age': '-1',
'friends':[('1', 'jim'),('t', 'bob'), ('3', 'joe'), ('4', 'fred')], 'friends': [('1', 'jim'), ('t', 'bob'), ('3', 'joe'), ('4', 'fred')],
'phones':[{'location':'bar', 'number':'555-1212'}, 'phones': [{'location': 'bar', 'number': '555-1212'},
{'location':'work', 'number':'555-8989'},], {'location': 'work', 'number': '555-8989'}],
} }
schema = Person() schema = Person()
schema.deserialize(cstruct) schema.deserialize(cstruct)
@@ -471,9 +471,9 @@ It will print something like:
.. code-block:: python .. code-block:: python
:linenos: :linenos:
Invalid: {'age':'-1 is less than minimum value 0', Invalid: {'age': '-1 is less than minimum value 0',
'friends.1.0':'"t" is not a number', 'friends.1.0': '"t" is not a number',
'phones.0.location:'"bar" is not one of "home", "work"'} 'phones.0.location': '"bar" is not one of "home", "work"'}
The above error is telling us that: The above error is telling us that:
@@ -494,11 +494,11 @@ dictionary:
import colander import colander
cstruct = { cstruct = {
'name':'keith', 'name': 'keith',
'age':'-1', 'age': '-1',
'friends':[('1', 'jim'),('t', 'bob'), ('3', 'joe'), ('4', 'fred')], 'friends': [('1', 'jim'), ('t', 'bob'), ('3', 'joe'), ('4', 'fred')],
'phones':[{'location':'bar', 'number':'555-1212'}, 'phones': [{'location': 'bar', 'number': '555-1212'},
{'location':'work', 'number':'555-8989'},], {'location': 'work', 'number': '555-8989'}],
} }
schema = Person() schema = Person()
try: try:
@@ -512,9 +512,9 @@ This will print something like:
.. code-block:: python .. code-block:: python
:linenos: :linenos:
{'age':'-1 is less than minimum value 0', {'age': '-1 is less than minimum value 0',
'friends.1.0':'"t" is not a number', 'friends.1.0': '"t" is not a number',
'phones.0.location:'"bar" is not one of "home", "work"'} 'phones.0.location': '"bar" is not one of "home", "work"'}
:exc:`colander.Invalid` Exceptions :exc:`colander.Invalid` Exceptions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -654,12 +654,12 @@ We can serialize a matching data structure:
.. code-block:: python .. code-block:: python
:linenos: :linenos:
appstruct = {'age':20, 'name':'Bob'} appstruct = {'age': 20, 'name': 'Bob'}
schema = Person() schema = Person()
serialized = schema.serialize(appstruct) serialized = schema.serialize(appstruct)
The value for ``serialized`` above will be ``{'age':'20', The value for ``serialized`` above will be ``{'age': '20',
'name':'Bob'}``. Note that the ``age`` integer has become a string. 'name': 'Bob'}``. Note that the ``age`` integer has become a string.
Serialization and deserialization are not completely symmetric, Serialization and deserialization are not completely symmetric,
however. Although schema-driven data conversion happens during however. Although schema-driven data conversion happens during
@@ -678,12 +678,12 @@ using the ``serialize`` method of the schema:
.. code-block:: python .. code-block:: python
:linenos: :linenos:
appstruct = {'age':20} appstruct = {'age': 20}
schema = Person() schema = Person()
serialized = schema.serialize(appstruct) serialized = schema.serialize(appstruct)
The value for ``serialized`` above will be ``{'age':'20', The value for ``serialized`` above will be ``{'age': '20',
'name':colander.null}``. Note the ``age`` integer has become a 'name': colander.null}``. Note the ``age`` integer has become a
string, and the missing ``name`` attribute has been replaced with string, and the missing ``name`` attribute has been replaced with
:attr:`colander.null`. Above, even though we did not include the :attr:`colander.null`. Above, even though we did not include the
``name`` attribute in the appstruct we fed to ``serialize``, an error ``name`` attribute in the appstruct we fed to ``serialize``, an error
@@ -1049,11 +1049,11 @@ a schema created declaratively:
:linenos: :linenos:
data = { data = {
'name':'keith', 'name': 'keith',
'age':'20', 'age': '20',
'friends':[('1', 'jim'),('2', 'bob'), ('3', 'joe'), ('4', 'fred')], 'friends': [('1', 'jim'), ('2', 'bob'), ('3', 'joe'), ('4', 'fred')],
'phones':[{'location':'home', 'number':'555-1212'}, 'phones': [{'location': 'home', 'number': '555-1212'},
{'location':'work', 'number':'555-8989'},], {'location': 'work', 'number': '555-8989'}],
} }
deserialized = schema.deserialize(data) deserialized = schema.deserialize(data)
@@ -1102,4 +1102,3 @@ module scope before mutating any of its subnodes:
:meth:`colander.SchemaNode.clone` clones all the nodes in the schema, :meth:`colander.SchemaNode.clone` clones all the nodes in the schema,
so you can work with a "deep copy" of the schema without disturbing the so you can work with a "deep copy" of the schema without disturbing the
"template" schema nodes defined at a higher scope. "template" schema nodes defined at a higher scope.