This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description

schemaconvertor提供了一种使用schema来转换对象的方法,通过schema,可以指定该对象序列化的部分和对应的类型,其结果可以进一步序列化为json。

安装:pip install schemaconvertor

项目:github pypi

版本:0.3

演示

假设有个简单的数据类型User

from collections import namedtuple

User = namedtuple("User", ["name", "password", "age"])

可以通过指定schema来转换对象:

schema = {
    "type": "object",
    "properties": {
        "name": {
            "type": "string"
        },
        "age": {
            "type": "integer"
        }
    }
}

user = User(name="lyc", password="schemaconvertor", age="24")

from schemaconvertor.convertor import convert_by_schema

print convert_by_schema(user, schema)

输出: > {‘age’: 24, ‘name’: ‘lyc’}

更多示例:demo 0.3

说明

基本字段

version

version字段标识着Schema版本。

description

description字段标识着Schema说明。

encoding

encoding指定Schema的string字段的字符编码,默认是utf-8

decoderrors

decoderrors指定Schema的string字段解码失败的操作,用于str.decode的第二个参数,主要有strictignorereplace三种可选参数,默认是strict

type

type字段指定对应待转换数据的最终类型,主要类型对应如下表:

type Python
string unicode
object dict
integer int
float float
number int/float
boolean bool
dict dict
array list
null NoneType
raw object

type字段直接影响转换行为,因此基本上每个Schema都需指定type,为简化表达,当一个Schema仅有type一项时,可以直接使用type的值简化表示为Schema。

typeOf

当前仅在声明typeOf字段时可以不指定typetypeOf指示如何根据数据的类型选择对应的Schema。可以使用真实的Python类型或类型元组作为key(作为isinstance的第二个参数)。

default

default字段仅用在typeOf字段内,用于指示缺省类型表示的Schema。

items

items字段仅在type为array时生效,用于描述序列中的每一项对应的Schema。

properties

items字段仅在type为dict或object时生效,指定给出的项的Schema(没有指定的项不会处理)。

patternProperties

items字段仅在type为dict或object时生效,指定符合给定的正则表达式的项的Schema(使用re.search匹配)。

附加信息

  1. Schema使用lazy compile方式,仅在转换使用时自动编译,初始化代价极小。
  2. 子Schema中如无显式声明,versiondescriptionencodingdecoderrors自动继承父Schema对应的值。
  3. typeOf能够识别继承关系,但针对使用数据真实类型的情况有优化。
  4. typeOf指定多种类型时不要使用list等非hashable类型。
  5. 对于object的情况是使用ObjAsDictAdapter将数据包装成类dict对象进行转换的。
Release History

Release History

0.3.1.0

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.3.0.2

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.3.0.1

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.3

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.2.2.5

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.2.2.4

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.2.2.3

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.2.2.2

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.2.2.1

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.2.2

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
schemaconvertor-0.3.1.0-py2.7.egg (22.9 kB) Copy SHA256 Checksum SHA256 2.7 Egg Apr 7, 2015
schemaconvertor-0.3.1.0.tar.gz (10.5 kB) Copy SHA256 Checksum SHA256 Source Apr 7, 2015

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS HPE HPE Development Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting