跳转到主要内容

无模式项目的模式助手

项目描述

摩菲斯帮助您为基于字典的类定义模式并执行验证、迁移和生成文档。它试图使这项通常令人不快的任务变得痛苦并易于处理。

设计目标

可读性。 像类定义顶部的文档字符串一样,模式定义应该是可读的并且可理解的。

透明性。 你知道如何处理字典。你应该能够继续使用字典。你应该能够在导入摩菲斯或省略它的情况下,代码仍然可以工作。

灵活性。 你可以选择使用哪种后端(sqlalchemy、sqlite、NoSQL等),因此摩菲斯在原生Python字典上操作,不接触你的数据存储。

功能

  • 使用Python惯用语直观定义模式

  • 可选的模式验证和检查,可以全局或按类控制

  • 从旧模式自动或按需迁移到当前模式

  • 为开发人员和用户生成模式文档

  • DRY定义模式。只做一次。在一个地方做。在任何地方使用(文档、数据存储和代码)

  • 无需任何依赖项

  • 单行启用/禁用

  • 通过所有字典的Python测试(包括json和pickle序列化)

  • 无痛苦、无痛苦、简单和容易!

安装

$ pip install morpheus

用法

以下是一个在基于字典的类上定义模式的简单示例。

# Let's import MorpheusDict as dict in our module
from morpheus import MorpheusDict as dict
# Note: Comment this last line out to completely disable morpheus. No code
# changes needed.


#
# Let's limit the keys allowed on our dict-based class by adding a __schema__
# entry
#
class Foo(dict):
    __schema__ = ['id', 'name', 'state']

#
# Now let's make sure this really works
#
try:
    Foo(sneaky='git blame someone for this!')
except AttributeError as exc:
    print "Thank you, Morpheus! You caught an error: %s" % exc

# Prints:
#
# Thank you, Morpheus! You caught an error: 'sneaky' is not a permitted
# attribute for a 'Foo'
#

以下是一个更复杂的示例,展示了多个模式、验证和迁移。

from morpheus import MorpheusDict, Schema, Defn

class Foo(dict):
    __schema__ = Schema(
        id=Defn(int, required=True),
        statoos=basestring,
        state=as_of(0.7).is_replaced_by('statoos')
    )

print Foo({'id': 1, 'state': 'DEPRECATED'})

# Prints:
#
# {'id': 1, 'statoos': 'DEPRECATED'}
#

Foo({})
# Generates ValidationError("Missing required key 'id'")

性能

要测试性能,请运行 python tests/test_performance.py

当前性能比原生dict慢约14倍。

与以下实现相比

  • 简单:1392.72517321%(从0.0004到0.006)

  • 子类:1358.45980888%(从0.0004到0.006)

  • 映射:439.377796719%(从0.001到0.006)

  • 列表:10094.0655908%(从0.0005到0.05)

  • 复杂:7229.4047619%(从0.0008到0.06)

贡献

Ziad Sawalha(ziadsawalha)是Morpheus的创建者和当前维护者。欢迎提交pull请求。

在提交pull请求之前,请确保您已添加/更新了适当的测试(并且所有现有测试在您的更改下仍然通过),并且您的编码风格遵循PEP 8,且不会导致pyflakes发出抱怨。

使用以下命令从markdown生成rst文档

::

pandoc –from=markdown –to=rst –output=README.rst README.md

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

morpheus-0.0.4.tar.gz (19.0 kB 查看散列

上传时间

支持者