Skip to main content

A wrapper around couchbase driver for document validation and more.

Project description

couchbasekit is a wrapper around couchbase 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 http://www.couchbase.com/ and about its Python driver form http://www.couchbase.com/develop/python/next.

Documentation: https://couchbasekit.readthedocs.org/en/latest/

Source code: https://github.com/kirpit/couchbasekit

Quick Start

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

from couchbasekit import Connection

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

Then define your model document.

author.py:

import datetime
from couchbasekit import Document
from couchbasekit.fields import EmailField, ChoiceField
from example.samples.publisher import Publisher
from example.samples.book 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, or many-to-many? some-to-some.. :)
        'has_book': bool,
        'age': int,
        'birthday': datetime.date,
        '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
        'slug',
        'first_name',
        'last_name',
        'email',
    )

Then use it as such;

>>> from example.samples.author import Author
>>> from couchbasekit.fields import EmailField
>>>
>>> douglas = Author()
>>> douglas.is_new_record
True
>>> 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)
True
>>> douglas.update({
...     'first_name': u'Douglas',
...     'last_name': u'Adams',
...     'email': EmailField('dna@example.com'),
... })
...
>>> douglas.validate()
True
>>> douglas.save()
14379837794698
>>> douglas.cas_value # CAS value (version) of the couchbase document
14379837794698
>>> douglas.id
u'douglas_adams'
>>> douglas.doc_id
u'author_douglas_adams'
>>> douglas.birthday is None
True
>>> douglas.non_exist_field
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "couchbasekit/document.py", 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
False
>>> douglas==dna
True
>>> douglas.has_book = True
>>> douglas==dna
False
>>> # 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.1.1.tar.gz (17.9 kB view details)

Uploaded Source

File details

Details for the file couchbasekit-0.1.1.tar.gz.

File metadata

File hashes

Hashes for couchbasekit-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c941e5e95d5158709099b13ceda350cf5da2e780c3b75b14d3acd93ad78f24dc
MD5 b95c97626bd4b7cb0c992ef048570453
BLAKE2b-256 f30ce3376d79144424145c4f2e54481a029236ace73a8f1fbb1f5513f82fb7b9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page