Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install monk

How to install monk

  1. Download and install ActivePython
  2. Buy and install the Business Edition license from account.activestate.com
  3. Open Command Prompt
  4. Type pypm install monk

monk contains builds that are only available via PyPM when you have a current ActivePython Business Edition subscription.

 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
0.5.0
0.5.1Never BuiltWhy not?
0.5.0 Available View build log
0.4.0 Available View build log
0.3.1 Available View build log
0.3.0 Available View build log
0.2.3 Available View build log
0.2.2 Available View build log
0.2.1 Available View build log
0.2.0 Available View build log
0.1.2 Available View build log
0.1.1 Available View build log
Windows (64-bit)
0.5.0
0.5.1Never BuiltWhy not?
0.5.0 Available View build log
0.4.0 Available View build log
0.3.1 Available View build log
0.3.0 Available View build log
0.2.3 Available View build log
0.2.2 Available View build log
0.2.1 Available View build log
0.2.0 Available View build log
0.1.2 Available View build log
0.1.1 Available View build log
Mac OS X (10.5+)
0.5.0
0.5.1Never BuiltWhy not?
0.5.0 Available View build log
0.4.0 Available View build log
0.3.1 Available View build log
0.3.0 Available View build log
0.2.3 Available View build log
0.2.2 Available View build log
0.2.1 Available View build log
0.2.0 Available View build log
0.1.2 Available View build log
0.1.1 Available View build log
Linux (32-bit)
0.5.1
0.5.1 Available View build log
0.5.0 Available View build log
0.4.0 Available View build log
0.3.1 Available View build log
0.3.0 Available View build log
0.2.3 Available View build log
0.2.2 Available View build log
0.2.1 Available View build log
0.2.0 Available View build log
0.1.2 Available View build log
0.1.1 Available View build log
Linux (64-bit)
0.5.1
0.5.1 Available View build log
0.5.0 Available View build log
0.4.0 Available View build log
0.3.1 Available View build log
0.3.0 Available View build log
0.2.3 Available View build log
0.2.2 Available View build log
0.2.1 Available View build log
0.2.0 Available View build log
0.1.2 Available View build log
0.1.1 Available View build log
 
License
GNU Lesser General Public License (LGPL), Version
Imports

An unobtrusive data modeling, manipulation and validation library.

Supports MongoDB out of the box. Can be used for any other DB (or even without one).

Installation

$ pip install monk

Dependencies

Monk is tested against the following versions of Python:

  • CPython 2.6, 2.7, 3.2, 3.3
  • PyPy 2.0

The MongoDB extension requires pymongo.

Documentation

See the complete documentation for details.

Examples

Modeling

The schema is defined as a template using native Python data types:

# we will reuse this structure in examples below

spec = {
    'title': 'Untitled',
    'comments': [
        { 'author': str,
          'date': datetime.datetime.utcnow,
          'text': str
        }
    ],
}

You are free to design as complex a document as you need. The manipulation and validation functions (described below) support arbitrary nested structures.

When this "natural" pythonic approach is not sufficient, you can mix it with a more verbose notation, e.g.:

title_spec = Rule(datatype=str, default='Untitled', validators=[...])

There are also neat shortcuts:

spec = {
    'url': optional(str),
    'status': one_of(['new', 'in progress', 'closed']),
    'blob': any_or_none,
    'price': optional(in_range(5, 200)),
}

By the way, the last one is translated into this one under the hood:

spec = {
    'price': Rule(datatype=int, optional=True,
                  validators=[monk.validators.validate_range(5, 200)]),
}

And, yes, you can mix notations. See FAQ.

This very short intro shows that Monk requires almost zero learning to start and then provides very powerful tools when you need them; you won't have to rewrite the "intuitive" code, only augment complexity exactly in places where it's inevitable.

Manipulation

The schema can be used to create full documents from incomplete data:

System Message: ERROR/3 (<string>, line 87)

Unknown directive type "code-block".

.. code-block:: python

    from monk.manipulation import merged

    # default values are set for missing keys

    >>> merge_defaults(spec, {})
    { 'title': 'Untitled',
      'comments': []
    }

    # it's easy to override the defaults

    >>> merge_defaults(spec, {'title': 'Hello'})
    { 'title': 'Hello',
      'comments': []
    }

    # nested lists of dictionaries can be auto-filled, too.
    # by the way, note the date.

    >>> merge_defaults(spec, {'comments': ['author': 'john']})
    { 'title': 'Untitled',
      'comments': [
            { 'author': 'john',
              'date': datetime.datetime(2013, 3, 3, 1, 8, 4, 152113),
              'text': None
            }
        ]
    }

Validation

The same schema can be used to ensure that the document has correct structure and the values are of correct types:

System Message: ERROR/3 (<string>, line 124)

Unknown directive type "code-block".

.. code-block:: python

    from monk.validation import validate

    # correct data: staying silent

    >>> validate(spec, data)

    # a key is missing

    >>> validate(spec, {'title': 'Hello'})
    Traceback (most recent call last):
       ...
    monk.errors.MissingKey: comments

    # a key is missing in a dictionary in a nested list

    >>> validate(spec, {'comments': [{'author': 'john'}]}
    Traceback (most recent call last):
       ...
    monk.errors.MissingKey: comments: #0: date

    # type check; also works with functions and methods (by return value)

    >>> validation.validate(spec, {'title': 123, 'comments': []})
    Traceback (most recent call last):
        ...
    TypeError: title: expected str, got int 123

Custom validators can be used. Behaviour can be fine-tuned.

The library can be also viewed as a framework for building ODMs (object-document mappers). See the MongoDB extension and note how it reuses mixins provided by DB-agnostic modules.

Here's an example of the MongoDB ODM bundled with Monk:

from monk.mongo import Document

class Item(Document):
    structure = dict(text=unicode, slug=unicode)
    indexes = dict(text=None, slug=dict(unique=True))

# this involves manipulation (inserting missing fields)
item = Item(text=u'foo', slug=u'bar')

# this involves validation
item.save(db)

Author

Originally written by Andrey Mikhaylenko since 2011.

Please feel free to submit patches, report bugs or request features:

http://bitbucket.org/neithere/monk/issues/

Licensing

Monk is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Monk is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with Monk. If not, see <http://gnu.org/licenses/>.

Subscribe to package updates

Download Stats

Last month:1

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.