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