couchbase-python

Couchbase Python SDK
Download

couchbase-python Ranking & Summary

Advertisement

  • Rating:
  • License:
  • The Apache License 2.0
  • Price:
  • FREE
  • Publisher Name:
  • Couchbase, Inc.
  • Publisher web site:
  • http://www.couchbase.com/

couchbase-python Tags


couchbase-python Description

couchbase-python is a Python library that provides methods to connect to both the couchbase memcached interface and the couchbase rest api interface.This version requires Python 2.6 or later.You'll need to install the following Python library requirements via pip: pip install requestsOpen Issues: http://www.couchbase.org/issues/browse/PYCBCUSAGETwo simple use cases to set and get a key in the default bucket and then create a new bucket using the memcached and rest clients:#!/usr/bin/env pythonfrom couchbase.couchbaseclient import CouchbaseClientfrom couchbase.couchbaseclient import MemcachedTimeoutExceptionfrom couchbase.rest_client import RestConnectionclient = CouchbaseClient("http://localhost:8091/pools/default", "default","",False)client.set("key1", 0, 0, "value1")client.get("key1")server_info = {"ip":"localhost", "port":8091, "username":"Administrator", "password":"password"}rest = RestConnection(server_info)rest.create_bucket(bucket='newbucket', ramQuotaMB=100, authType='none', saslPassword='', replicaNumber=1, proxyPort=11215, bucketType='membase')Example code that creates buckets and then does sets, gets and views using the unified client:import couchbase# connect to a couchbase servercb = couchbase.Server('localhost:8091', username='Administrator', password='password')# create default bucket if it doesn't existtry: cb.create('default')except: pass# fetch a Bucket with subscriptdefault_bucket = cb# set a value with subscript (equivilent to .set)default_bucket = 'value1'# fetch a bucket with a functiondefault_bucket2 = cb.bucket('default')# set a json value with subscript (equivilent to .set)default_bucket2 = {'value':'value2','expiration':0,'flags':10}# set a value with a functiondefault_bucket.set('key3', 0, 0, 'value3')# fetch a key with a functionprint 'key1 ' + str(default_bucket.get('key1'))print 'key2 ' + str(default_bucket2.get('key2'))# fetch a key with subscriptprint 'key3 ' + str(default_bucket2)# delete a bucketcb.delete('default')try: cbexcept Exception as ex: print ex# create a new buckettry: newbucket = cb.create('newbucket', ram_quota_mb=100, replica=1)except: newbucket = cb# set a json document with a function# this will translate $flags and $expiration to memcached protocol# automatically generate the _iddoc_id = newbucket.save({'type':'item', 'value':'json test', '$flags':25})print doc_id + ' ' + str(newbucket)# use a provided _iddoc_id = newbucket.save({'_id':'key4', 'type':'item', 'value':'json test', '$flags':25})print doc_id + ' ' + str(newbucket)design = { "_id" : "_design/testing", "language" : "javascript", "views" : { "all" : { "map" : '''function (doc) {\n emit(doc, null);\n}''' }, }, }# save a design document# right now with no _rev, we can only create, we can't updatetry: doc_id = newbucket.save(design)except: doc_id = "_design/testing"rows = newbucket.view("_design/testing/_view/all")for row in rows: print rowRUNNING TESTSRequirements:- easy_install nose- pip install nose-testconfigWe're now using nose to run our tests. There's a supplied test.ini.template that you can customize to match your installed environment. Copy test.ini.template to test.ini, customize, and then run the following command: nosetests --tc-file=test.iniAdding coverage information is as easy as install coverage and running nosetests with these settings: nosetests --tc-file=test.ini --with-coverage --cover-package=couchbase --cover-htmlThis will output coverage reports into the 'cover' directory.Product's homepage


couchbase-python Related Software