Skip to main content

Morpheus Build Status Coverage Status

Morpheus helps you define schemas for your dict-based classes and perform validation, migration, and generate documentation. It tries to make this normally unfun task painless and easy.

Design Goals

Readable. Like a docstring at the top of your class definition, a schema definition should be readable and understandable.

Transparent. You know how to work with dicts. You should be able to keep using dicts. And you should be able to import morpheus or leave it out and your code should still work.

Flexible. You get to pick which backend you use (sqlalchemy, sqlite, NoSQL, etc…), so morpheus operates on native python dicts and doesn’t touch your data store.

Features

  • Intuitive schema definition using pythonic idioms

  • Optional schema validation and inspection controlled globally or per class

  • Automatic or on-demand migration from an older schema to the current one

  • Generates schema documentation for developers and users

  • DRY definition of schemas. Do it once. Do it in one place. Use it anywhere (docs, data store, and code)

  • Zero dependencies required

  • Enable/disable it with a single line

  • Passes all python tests for a dict (including json and pickle serialization)

  • Painless, pain-free, simple and easy!

Install

$ pip install morpheus

Usage

Here is a simple example of a schema definition on a dict-based class.

# 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'
#

Here is a more involved example, demonstrating multiple schemas, validation, and migration.

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'")

Performance

To test performance, run python tests/test_performance.py

The current performance is ~14 times slower than native dict.

Versus the following implementations:

  • Simple: 1392.72517321% (from 0.0004 to 0.006)

  • Subclass: 1358.45980888% (from 0.0004 to 0.006)

  • Mapping: 439.377796719% (from 0.001 to 0.006)

  • List: 10094.0655908% (from 0.0005 to 0.05)

  • Complex: 7229.4047619% (from 0.0008 to 0.06)

Contributing

Ziad Sawalha (ziadsawalha) is the creator and current maintainer of Morpheus. Pull requests are always welcome.

Before submitting a pull request, please ensure you have added/updated the appropriate tests (and that all existing tests still pass with your changes), and that your coding style follows PEP 8 and doesn’t cause pyflakes to complain.

rst doc generated from markdown with:

::

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

Metadata

Release files for morpheus 0.0.4

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

Source distribution (sdist)

Source distribution for morpheus 0.0.4
File Size Uploaded
morpheus-0.0.4.tar.gz 19.0 kB Details

Release files / morpheus-0.0.4.tar.gz

Download URL morpheus-0.0.4.tar.gz
Size 19.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d6898f3333fb5cc76ce504ee955a56029c09e95e1c6dbd0ea660e9dcbd9a6628
BLAKE2b-256 checksum
How to use checksums
98843254353979f354fc492fcc81b2a6c8cf6a24e221e99fa14b64c49a4b1c59
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.4 This release

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

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