Skip to main content

A wrapper around CouchBase Python driver for document validation and more.

Project description

couchbasekit is a wrapper around CouchBase Python driver for document validation and more. It was inspired by MongoKit and was developed by the project coming soon?, which is also an open source project.

You can get detailed information about couchbase itself from and about its Python driver form


Source code:

Quick Start

Less talk, more code. Set your authentication details first:

from couchbasekit import Connection

# you should do this somewhere beginning such as
Connection.auth('myusername', 'p@ssword')

Then define your model document.

import datetime
from couchbasekit import Document
from couchbasekit.fields import EmailField, ChoiceField
from example.samples.publisher import Publisher
from import Book

class Gender(ChoiceField):
    CHOICES = {
        'M': 'Male',
        'F': 'Female',

class Author(Document):
    __bucket_name__ = 'couchbasekit_samples'
    __key_field__ = 'slug' # optional
    doc_type = 'author'
    structure = {
        'slug': unicode,
        'first_name': unicode,
        'last_name': unicode,
        'gender': Gender,
        'email': EmailField,
        'publisher': Publisher, # kind of foreign key
        'books': [Book], # 1-to-many
        'has_book': bool,
        'age': int,
        'created_at': datetime.datetime,
    default_values = { # optional
        'has_book': False,
        # don't worry about the timezone info!
        # it's auto assigned as to UTC, so all you have to do is:
        'created_at': datetime.datetime.utcnow,
    required_fields = ( # optional

Then use it as such;

>>> from import Author
>>> from couchbasekit.fields import EmailField
>>> douglas = Author()
>>> douglas.is_new_record
>>> try:
...     douglas.validate()
... except Author.StructureError as why:
...     print why
Key field 'slug' is defined but not provided.
>>> douglas.slug = u'douglas_adams'
>>> try:
...     douglas.validate()
... except Author.StructureError as why:
...     print why
Required field for 'first_name' is missing.
>>> isinstance(douglas, dict)
>>> douglas.update({
...     'first_name': u'Douglas',
...     'last_name': u'Adams',
...     'gender': Gender('M'),
...     'email': EmailField(''),
... })
>>> douglas.validate()
>>> douglas.cas_value # CAS value (version) of the couchbase document
>>> douglas.doc_id
>>> douglas.birthday is None
>>> douglas.non_exist_field
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "couchbasekit/", line 68, in __getattr__
    return super(Document, self).__getattribute__(item)
AttributeError: 'Author' object has no attribute 'non_exist_field'
>>> dna = Author('douglas_adams')
>>> dna.is_new_record
>>> douglas==dna
>>> douglas.has_book = True
>>> douglas==dna
>>> # nice!

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

couchbasekit-0.2.1.tar.gz (15.0 kB view hashes)

Uploaded source

Supported by

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