Package: pycouchbase
Why add another layer to the SDK?Inconsistent data types in Couchbase documents can be a real pain.Better management of multiple Couchbase connections.Readability of Couchbase documents in the Python source code while still being able to use Couchbase SDK’s operations.FeaturesThis was originally forked from and inspired bycouchbasekit.Validate Couchbase documents.Represent Couchbase documents as Python objects.Easily manage multiple Couchbase connections.SupportsCouchbase Python SDK 2.0operations.There are data retrieval operations that are already included in couchbasekit but I haven’t thoroughly tested it with PyCouchbase.InstallationAt the command line:$ easy_install pycouchbaseOr, if you have virtualenvwrapper installed:$ mkvirtualenv pycouchbase
$ pip install pycouchbaseGetting StartedLet us go through a simple example.Import everything we need:importdatetimefrompycouchbaseimportConnectionfrompycouchbaseimportDocument,register_viewfrompycouchbase.fieldsimportEmailField,ChoiceFieldDeclare the Document class:# You can define your own field/data typeclassGender(ChoiceField):CHOICES={'M':'Male','F':'Female',}@register_view('dev_authors')classAuthor(Document):__bucket_name__='couchbasekit_samples'__key_field__='slug'# optionaldoc_type='author'structure={'slug':unicode,'first_name':unicode,'last_name':unicode,'gender':Gender,'email':EmailField,'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',)Validate and save your document:local_connection=Connection.auth(server='localhost')author=Author()bucket=author.get_bucket(local_connection)author.update({'slug':u'douglas_adams','first_name':u'Douglas','last_name':u'Adams','gender':Gender('M'),'email':EmailField('dna@example.com'),})# Try to validate before savingtry:author.validate()try:rvs=bucket.insert(author.slug,author.encode())exceptKeyExistsErroraswhy:print(why)exceptAuthor.StructureErroraswhy:# when the data structure is invalidprint(why)Save multiple documents:local_connection=Connection.auth(server='localhost')author=Author()list_data=[{'slug':u'douglas_adams','first_name':u'Douglas','last_name':u'Adams','gender':Gender('M'),'email':EmailField('dna@example.com'),},{'slug':u'isaac_asimov','first_name':u'Isaac','last_name':u'Asimov','gender':Gender('M'),'email':EmailField('dna@example.com'),}]try:bucket=author.get_bucket(local_connection)updated_authors={}fordinlist_data:author.update(d)try:# validate!author.validate()updated_authors.update({d['slug']:author.encode()})exceptauthor.StructureErroraswhy:print(why)# save multiple datarvs=bucket.upsert_multi(updated_authors)exceptCouchbaseNetworkErroraswhy:print(why)Manage multiple connections:connection_1=Connection.auth(server='server_1')connection_2=Connection.auth(server='server_2')# where doc_1 and doc_2 are document objectsbucket_1=doc_1.get_bucket(connection_1)bucket_2=doc_2.get_bucket(connection_2)Bucket objects can support anyCouchbase Python SDK 2.0operations:bucket_1.get('key_or_id')bucket_1.insert('key_or_id',value)More about Couchbase SDK’s supported operations here:http://docs.couchbase.com/developer/python-2.0/introduction.htmlHistory0.1.0 (2015-01-11)First release on PyPI.