Skip to main content

Convert python dict (YAML, JSON) to colander schema.

Colander is library, which enables you to validate and convert YAML, JSON and HTML form data. Colander schema as it is could be defined only in Python declaratively for example like this:

import colander

class Person(colander.MappingSchema):
    name = colander.SchemaNode(colander.String())
    age = colander.SchemaNode(colander.Int(),
                              validator=colander.Range(0, 200))

    @colander.instantiate()
    class phones(colander.SequenceSchema):

        @colander.instantiate()
        class phone(colander.MappingSchema):
            location = colander.SchemaNode(colander.String(),
                                 validator=colander.OneOf(['home', 'work']))
            number = colander.SchemaNode(colander.String())

With dict2colander you can define the same colander schema in dictionary and convert it to colader.MappingSchema object.

Instalation

You can install dict2colander from Python Package Index like this:

pip install dict2colander

Usage

from dict2colander import dict2colander

schema_dict = {
    'type': 'Mapping',
    'name': 'person',
    'subnodes': [
        {'type': 'String', 'name': 'name'},

        {'type': 'Integer', 'name': 'age',
         'validators':
            {'Range': {'args': ('0', '200')}}},

        {'type': 'Sequence',
         'name': 'phones',
         'subnodes': [

             {'type': 'Mapping', 'name': 'phone',
              'subnodes': [

                  {'type': 'String', 'name': 'location',
                   'validators':
                     {'OneOf': {'args': (['home', 'work'],)}}},

                  {'type': 'String', 'name': 'number'}

              ]}]},
    ]
}

schema = dict2colander(schema_dict)
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'},],
         }

serialized_data = schema.deserialize(data)
print serialized_data

Dict2colander is intended to make possible to read colander schemas from YAML or JSON format. So here is schema from previous example written in YAML:

---
name: person
type: Mapping

subnodes:
    - name: name
      type: String

    - name: age
      type: Integer
      validators:
        Range:
            args: ['0', '200']

    - name: phones
      type: Sequence
      subnodes:
        - name: phone
          type: Mapping
          subnodes:
            - name: location
              type: String
              validators:
                OneOf:
                    args: [[home, work]]

            - name: number
              type: String

Note that Range validator has arguments defined as Strings not Integers although that field age is of type Integer.

Here are data to deserialize in YAML format from first example:

---
name: keith
age: 20
friends:
    - [1, jim]
    - [2, bob]
    - [3, joe]
    - [4, fred]

phones:
    - location: home
      number: 555-1212

    - location: work
      number: 555-8989

Here is example how YAML data are deserialized with schema defined in YAML document.

import yaml
import dict2colander

def deserialize(yaml_doc, yaml_schema):
    mapping_schema = dict2colander.dict2colander(yaml_schema)
    return mapping_schema.deserialize(yaml_doc)

f = open('doc.yaml')
doc = yaml.load(f)
f.close()

f = open('schema.yaml')
schema = yaml.load(f)
f.close()

dict_doc = deserialize(doc, schema)
print dict_doc

Release files for dict2colander 0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dict2colander 0.2
File Size Uploaded
dict2colander-0.2.tar.gz 7.4 kB Details

Release files / dict2colander-0.2.tar.gz

Download URL dict2colander-0.2.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
6f668d60896991dcd271465b755f00ffd6f87f81e0d4d054be62a16c086978c7
BLAKE2b-256 checksum
How to use checksums
aa7e5ed2ba3dc2f06457b76d4bc8c93559179472bf87e6982f9a9e5cea30e84e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2 This release

1 release file

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page