Skip to main content

Dict schema helper for schema-free projects

Project description


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.

# Comment this line out to completely disable morpheus. No code changes needed.
from morpheus import MorpheusDict as dict

class Foo(dict):
__schema__ = ['id', 'name', 'state']

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.


import morpheus


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

Commit messages should be formatted using [AngularJS conventions][ajs] (one-liners are OK for now but body and footer may be required as the project matures).

Comments follow [Google's style guide][goog-style-comments].


### Legal ###

Copyright 2013 by Ziad Sawalha.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.

### Darn, this is tough! ###

Here are some areas where morpheus under-delivers:

- Replacing dict with a MorpheusDict will cause any tests you have against your
dict-based classes that call assertDictEqual to fail. assertDictEqual checks if
isinstance(, dict) and fails on a MorpheusDict

Project details

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page