Skip to main content

Dictionary to Colander schema conversion library

Project description

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 = {
    'typ': 'Mapping',
    'name': 'person',
    'children': [
        {'typ': 'String', 'name': 'name'},

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

        {'typ': 'Sequence',
         'name': 'phones',
         'children': [

             {'typ': 'Mapping', 'name': 'phone',
              'children': [

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

                  {'typ': '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
typ: Mapping

children:
    - name: name
      typ: String

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

    - name: phones
      typ: Sequence

      children:
        - name: phone
          typ: Mapping

          children:
            - name: location
              typ: String
              validators:
                OneOf:
                    args: [[home, work]]

            - name: number
              typ: 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dict2colander-0.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dict2colander-0.1.0-py2.7.egg (11.7 kB view details)

Uploaded Egg

File details

Details for the file dict2colander-0.1.tar.gz.

File metadata

  • Download URL: dict2colander-0.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for dict2colander-0.1.tar.gz
Algorithm Hash digest
SHA256 260bea4496df0118a5f87abbaaad04db45d87bc9b4ec22d4452165e7f7c49c67
MD5 33e5f9c5ba5827b80c2bff22291908b9
BLAKE2b-256 fb6287b147d5eebb1bc6020f16362788b798f7c74be88ebcd2656234143e607e

See more details on using hashes here.

File details

Details for the file dict2colander-0.1.0-py2.7.egg.

File metadata

File hashes

Hashes for dict2colander-0.1.0-py2.7.egg
Algorithm Hash digest
SHA256 b7aca697c3f6f0bc8dc718f6a6cc883e3d350b2aada3df581916b2168e484f8f
MD5 840c433736f54881ef8416af86e97af9
BLAKE2b-256 24d069d7a6bce43a38e2a90bcc2a9ed8dcecf1272d65490a8eac96bba0e0926b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page