Fetching contributors…
Cannot retrieve contributors at this time
117 lines (86 sloc) 3.37 KB

Manipulating Data Structures

Colander schemas have some utility functions which can be used to manipulate an :term:`appstruct` or a :term:`cstruct`. Nested data structures can be flattened into a single dictionary or a single flattened dictionary can be used to produce a nested data structure. Values of particular nodes can also be set or retrieved based on a flattened path spec.

Flattening a Data Structure

:meth:`colander.SchemaNode.flatten` can be used to convert a datastructure with nested dictionaries and/or lists into a single flattened dictionary where each key in the dictionary is a dotted name path to the node in the nested structure.

Consider the following schema:

Consider also a particular serialization of data using that schema:

This data can be flattened:

The resulting flattened structure would look like this:

The process can be reversed using :meth:`colandar.SchemaNode.unflatten`:

Either an :term:`appstruct` or a :term:`cstruct` can be flattened or unflattened in this way.

Accessing and Mutating Nodes in a Data Structure

:attr:`colander.SchemaNode.get_value` and :attr:`colander.SchemaNode.set_value` can be used to access and mutate nodes in an :term:`appstruct` or :term:`cstruct`. Using the example from above: