Skip to main content

Python Client for Couchbase

Project description

Client for Couchbase.

https://travis-ci.org/couchbase/couchbase-python-client.png

Building and Installing

This only applies to building from source. If you are using a Windows installer then everything (other than the server) is already included. See below for windows snapshot releases.

Also note that these instructions apply to building from source. You can always get the latest supported release version from pypi.

If you have a recent version of pip, you may use the latest development version by issuing the following incantation

pip install git+git://github.com/couchbase/couchbase-python-client

Prerequisites

Building

The following will compile the module locally; you can then test basic functionality including running the examples.

python setup.py build_ext --inplace

If your libcouchbase install is in an alternate location (for example, /opt/local/libcouchbase), you may add extra directives, like so

python setup.py build_ext --inplace \
    --library-dir /opt/local/libcouchbase/lib \
    --include-dir /opt/local/libcouchbase/include

Or you can modify the environment CFLAGS and LDFLAGS variables.

Installing

python setup.py install

Using

Here’s an example code snippet which sets a key and then reads it

>>> from couchbase.bucket import Bucket
>>> c = Bucket('couchbase://localhost/default')
>>> c
<couchbase.bucket.Bucket bucket=default, nodes=['localhost:8091'] at 0x105991cd0>
>>> c.upsert("key", "value")
OperationResult<RC=0x0, Key=key, CAS=0x31c0e3f3fc4b0000>
>>> res = c.get("key")
>>> res
ValueResult<RC=0x0, Key=key, Value=u'value', CAS=0x31c0e3f3fc4b0000, Flags=0x0>
>>> res.value
u'value'
>>>

You can also use views

>>> from couchbase.bucket import Bucket
>>> c = Bucket('couchbase://localhost/beer-sample')
>>> resultset = c.query("beer", "brewery_beers", limit=5)
>>> resultset
View<Design=beer, View=brewery_beers, Query=Query:'limit=5', Rows Fetched=0>
>>> for row in resultset: print row.key
...
[u'21st_amendment_brewery_cafe']
[u'21st_amendment_brewery_cafe', u'21st_amendment_brewery_cafe-21a_ipa']
[u'21st_amendment_brewery_cafe', u'21st_amendment_brewery_cafe-563_stout']
[u'21st_amendment_brewery_cafe', u'21st_amendment_brewery_cafe-amendment_pale_ale']
[u'21st_amendment_brewery_cafe', u'21st_amendment_brewery_cafe-bitter_american']

Twisted API

The Python client now has support for the Twisted async network framework. To use with Twisted, simply import txcouchbase.connection instead of couchbase.bucket

from twisted.internet import reactor
from txcouchbase.bucket import Bucket

cb = Bucket('couchbase://localhost/default')
def on_upsert(ret):
    print "Set key. Result", ret

def on_get(ret):
    print "Got key. Result", ret
    reactor.stop()

cb.upsert("key", "value").addCallback(on_upsert)
cb.get("key").addCallback(on_get)
reactor.run()

# Output:
# Set key. Result OperationResult<RC=0x0, Key=key, CAS=0x9a78cf56c08c0500>
# Got key. Result ValueResult<RC=0x0, Key=key, Value=u'value', CAS=0x9a78cf56c08c0500, Flags=0x0>

The txcouchbase API is identical to the couchbase API, except that where the synchronous API will block until it receives a result, the async API will return a Deferred which will be called later with the result or an appropriate error.

GEvent API

from gcouchbase.bucket import Bucket

conn = Bucket('couchbase://localhost/default')
print conn.upsert("foo", "bar")
print conn.get("foo")

The API functions exactly like the normal Bucket API, except that the implementation is significantly different.

Asynchronous (Tulip) API

This module also supports Python 3.4/3.5 asynchronous I/O. To use this functionality, import the couchbase.experimental module (since this functionality is considered experimental) and then import the acouchbase module. The acouchbase module offers an API similar to the synchronous client:

import asyncio

import couchbase.experimental
couchbase.experimental.enable()
from acouchbase.bucket import Bucket


async def write_and_read(key, value):
    cb = Bucket('couchbase://10.0.0.31/default')
    await cb.connect()
    await cb.upsert(key, value)
    return await cb.get(key)

loop = asyncio.get_event_loop()
rv = loop.run_until_complete(write_and_read('foo', 'bar'))
print(rv.value)

PyPy

PyPy is an alternative high performance Python implementation. Since PyPy does not work well with C extension modules, this module will not work directly. You may refer to the alternate implementation based on the cffi module: https://github.com/couchbaselabs/couchbase-python-cffi

Other Examples

There are other examples in the examples directory. To run them from the source tree, do something like

PYTHONPATH=$PWD ./examples/bench.py -U couchbase://localhost/default

Building documentation

The documentation is using Sphinx and also needs the numpydoc Sphinx extension. To build the documentation, go into the docs directory and run

make html

The HTML output can be found in docs/build/html/.

Testing

For running the tests, you need the standard unittest module, shipped with Python. Additionally, the testresources package is required.

To run them, use either py.test, unittest or trial.

The tests need a running Couchbase instance. For this, a tests.ini file must be present, containing various connection parameters. An example of this file may be found in tests.ini.sample. You may copy this file to tests.ini and modify the values as needed.

The simplest way to run the tests is to declare a bucket_prefix in the tests.ini file and run the setup_tests.py script to create them for you.

python setup_tests.py

To run the tests:

nosetests

Support & Additional Resources

If you found an issue, please file it in our JIRA. You can ask questions in our forums or in the #libcouchbase channel on freenode.

The official documentation can be consulted as well for general Couchbase concepts and offers a more didactic approach to using the SDK.

License

The Couchbase Python SDK is licensed under the Apache License 2.0.

Project details


Release history Release notifications | RSS feed

This version

2.1.1

Download files

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

Source Distribution

couchbase-2.1.1.tar.gz (236.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

couchbase-2.1.1.win-amd64-py3.4.exe (683.2 kB view details)

Uploaded Source

couchbase-2.1.1.win-amd64-py3.3.exe (683.4 kB view details)

Uploaded Source

couchbase-2.1.1.win-amd64-py3.2.exe (691.9 kB view details)

Uploaded Source

couchbase-2.1.1.win-amd64-py2.7.exe (691.5 kB view details)

Uploaded Source

couchbase-2.1.1.win32-py3.4.exe (610.0 kB view details)

Uploaded Source

couchbase-2.1.1.win32-py3.3.exe (610.1 kB view details)

Uploaded Source

couchbase-2.1.1.win32-py3.2.exe (619.1 kB view details)

Uploaded Source

couchbase-2.1.1.win32-py2.7.exe (619.3 kB view details)

Uploaded Source

File details

Details for the file couchbase-2.1.1.tar.gz.

File metadata

  • Download URL: couchbase-2.1.1.tar.gz
  • Upload date:
  • Size: 236.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for couchbase-2.1.1.tar.gz
Algorithm Hash digest
SHA256 149fa3b41581f8a9e2b1999392c7bdbd9d64854fc219d06ae3aa79434d8d35f4
MD5 e4525775540e085aad770449c4015c04
BLAKE2b-256 34cfa5ba710fcd798359777ee190939f6d896a407b36998f262a783172e96ad5

See more details on using hashes here.

File details

Details for the file couchbase-2.1.1.win-amd64-py3.4.exe.

File metadata

File hashes

Hashes for couchbase-2.1.1.win-amd64-py3.4.exe
Algorithm Hash digest
SHA256 27fc185c0075fe4edf9d1b14ea81a75153411673bad8d25a2039ec4459ca7ccd
MD5 82bfa5a9b650bcf9370f2e26248e184f
BLAKE2b-256 4599fd9d99a097099a7ffd7b25a566b93a99e09f48716f7fbb642516e15391dd

See more details on using hashes here.

File details

Details for the file couchbase-2.1.1.win-amd64-py3.3.exe.

File metadata

File hashes

Hashes for couchbase-2.1.1.win-amd64-py3.3.exe
Algorithm Hash digest
SHA256 fad4ca1172fb677078f4c4f14aa8d13563cea1ca5e37dd7e00261e9c6f5ada62
MD5 1f73599ae5957cb1b82923c5c9236e3d
BLAKE2b-256 9c2b50c1a9fa279922a522a143577cca21edb41ecd5dc05fdb2def31c2fc4ef1

See more details on using hashes here.

File details

Details for the file couchbase-2.1.1.win-amd64-py3.2.exe.

File metadata

File hashes

Hashes for couchbase-2.1.1.win-amd64-py3.2.exe
Algorithm Hash digest
SHA256 d2b52d0118b55aef5858358a955f8516f64e23f3960ae90d0802481f63005de3
MD5 49e861e2efea2a4a82b658a47cddda9b
BLAKE2b-256 fe26e3a69171cd3d5ec98ba27575490f64f588afc596b2fb3d3811a3a161d00d

See more details on using hashes here.

File details

Details for the file couchbase-2.1.1.win-amd64-py2.7.exe.

File metadata

File hashes

Hashes for couchbase-2.1.1.win-amd64-py2.7.exe
Algorithm Hash digest
SHA256 9a2a700d014af4502d4701d16881d2e4aaf2356c8ce5f0c756d9fedda2b84d57
MD5 4e19dd037497c07a2662cc87c9625343
BLAKE2b-256 e787be6f720b49350406cb346513b48a9561db9fc808d089288f382751611a24

See more details on using hashes here.

File details

Details for the file couchbase-2.1.1.win32-py3.4.exe.

File metadata

File hashes

Hashes for couchbase-2.1.1.win32-py3.4.exe
Algorithm Hash digest
SHA256 74823c37a2e3c5815f011dfbc3bd3845ceee9ebd4b8344890b6ffe0afd405e3f
MD5 77a75b150edc4a5a5abcb1a4ec1874c2
BLAKE2b-256 fd75487beccc3a611ba022e1c9e5b706d2b37666e56286002eee4271b19cdeca

See more details on using hashes here.

File details

Details for the file couchbase-2.1.1.win32-py3.3.exe.

File metadata

File hashes

Hashes for couchbase-2.1.1.win32-py3.3.exe
Algorithm Hash digest
SHA256 9c992c2e0cb508d3ebc0c4282fdafe0340906a30950e99f1d459c7249116d3ff
MD5 751e4380f088e14e88c93a3bd07cc3b6
BLAKE2b-256 57340dc57cea81574b5d7fe4f4e8f63060981d3b6cfe65a418b4d29e802f4f38

See more details on using hashes here.

File details

Details for the file couchbase-2.1.1.win32-py3.2.exe.

File metadata

File hashes

Hashes for couchbase-2.1.1.win32-py3.2.exe
Algorithm Hash digest
SHA256 73a58e3b243148717bb95841ee797469eb9f2af3dbe632085f67e1a3b754f3dc
MD5 0e134f24abc32030bea4a81689d26d3d
BLAKE2b-256 8192e9359f5ed5c18fe46b427c4b8770b8301f7fcd26e08a435d8019a171eb0b

See more details on using hashes here.

File details

Details for the file couchbase-2.1.1.win32-py2.7.exe.

File metadata

File hashes

Hashes for couchbase-2.1.1.win32-py2.7.exe
Algorithm Hash digest
SHA256 1b01722eae4fa054e047bbfa3c57a1b246c44ca96a5d120e8e4cea2fa07b43a3
MD5 59262c331e59964338c0e24d0a3c95c1
BLAKE2b-256 13e4f7f757b14b186c5b0b3ca57ce4ede014e70ed10a31ce28954625a4f12083

See more details on using hashes here.

Supported by

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