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. In order for the documentation to build properly, the C extension must have been built, since there are embedded docstrings in there as well.

To build the documentation, go into the docs directory and run

make html

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

Alternatively, you can also build the documentation (after building the module itself) from the top-level directory:

python setup.py build_sphinx

Once built, the docs will be in in build/sphinx/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.2.4

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.2.4.tar.gz (243.6 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.2.4.win-amd64-py3.4.exe (722.8 kB view details)

Uploaded Source

couchbase-2.2.4.win-amd64-py3.3.exe (722.9 kB view details)

Uploaded Source

couchbase-2.2.4.win-amd64-py3.2.exe (732.8 kB view details)

Uploaded Source

couchbase-2.2.4.win-amd64-py2.7.exe (734.1 kB view details)

Uploaded Source

couchbase-2.2.4.win32-py3.4.exe (644.9 kB view details)

Uploaded Source

couchbase-2.2.4.win32-py3.3.exe (645.1 kB view details)

Uploaded Source

couchbase-2.2.4.win32-py3.2.exe (652.0 kB view details)

Uploaded Source

couchbase-2.2.4.win32-py2.7.exe (653.8 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for couchbase-2.2.4.tar.gz
Algorithm Hash digest
SHA256 a0f9a4e92d71d97fff772fd3e477b671b88274718efcace23c66a5bdde055b1f
MD5 60f23fa8846421620d7a8886d62adc17
BLAKE2b-256 cfee5011f35ede6b7db376d5c27536bc5f2f7be14ba03b513698cb809babbb97

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for couchbase-2.2.4.win-amd64-py3.4.exe
Algorithm Hash digest
SHA256 7c06e4cca5ac3fc3d7bdeaac9fb37cdf987c0cc059a0525638c51f717f252518
MD5 05695d646fa57c6d1c31d24d15a70fa2
BLAKE2b-256 d6aef109c858823b64ab2571e63416ef843c6551cc166af853401c8c1f2e50ee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for couchbase-2.2.4.win-amd64-py3.3.exe
Algorithm Hash digest
SHA256 2f635591e63fdaa5f9579d64022da58e4d4dad755ddf6e897920b1c7bb7b82eb
MD5 5c9268002614ac5f1fb7b82d2c4e287a
BLAKE2b-256 b40dff8b3ab0384917ea30720f76c17c870c3d7e4c0cb84f963e00a652ca4f0d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for couchbase-2.2.4.win-amd64-py3.2.exe
Algorithm Hash digest
SHA256 f2add60c619db9f64bce613304788d2fe459f477308fe4b5c0bde914c4715990
MD5 40b83ca078677cb643398d440f458369
BLAKE2b-256 b99243c1c5da8c3fb93509f3cd4f09ed13bb8523e8601e033709c497cc9521a2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for couchbase-2.2.4.win-amd64-py2.7.exe
Algorithm Hash digest
SHA256 ef1ce7a6fcd2f5cb1e25c04e6858d2ccc0aed36f16159b7099935f348eb8bb4b
MD5 b8f078df20f1fde544ab90d7624e35c7
BLAKE2b-256 5791e4fbb681197b12d389e88678641d3f78829dfac9f1e78afb5b650aedf2d8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for couchbase-2.2.4.win32-py3.4.exe
Algorithm Hash digest
SHA256 6710da9ec33864faa00d72ef6320097431704a4c282e6f7de287ed5d33a14820
MD5 8e9236a19f9858001f9096e3b6cc1613
BLAKE2b-256 a910176fee485c822156da9f92ba0c166dcc10239da57e86bfcde608356cfb8a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for couchbase-2.2.4.win32-py3.3.exe
Algorithm Hash digest
SHA256 74b8224c4afb35241cfd95c95ff65e963048294efd8c4f427265ee105f48f353
MD5 42a23d7609f6602b465de864ad07cf14
BLAKE2b-256 95430997e33740d053d2da8d359442d07bd242b479a3d6a2ec4aee26f205a907

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for couchbase-2.2.4.win32-py3.2.exe
Algorithm Hash digest
SHA256 d90025d1e369e1a8447e12cf5bd3c3926ac251361aaf2037a830ebdb406b0b46
MD5 7cd9e6a83278a622aa2ea94bc802ddd6
BLAKE2b-256 e411ea53127b561a43b8dba9536621d84722cc7878ea6a19c4d5ef5e99f9d74a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for couchbase-2.2.4.win32-py2.7.exe
Algorithm Hash digest
SHA256 94750409a4d7bc2018dab311a8a514c48b8255dcb751f1902594ee3348dc4ec0
MD5 90cf206d92947a6306b857700305baf5
BLAKE2b-256 778232f183b45b459501d69962607471de0eef8b393f88d4364069b7a9221bef

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