Python Client for Couchbase
Project description
Client for Couchbase.
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
Prerequisites
Couchbase Server (http://couchbase.com/download)
libcouchbase. version 2.1.0 or greater (Bundled in Windows installer)
libcouchbase development files.
Python development files
A C compiler.
Building
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.
Windows Snapshots
A list of recent snapshot builds for Windows may be found here <http://packages.couchbase.com/clients/python/snapshots>.
You can always get release binaries from PyPi (as above).
Using
Here’s an example code snippet which sets a key and then reads it
>>> from couchbase import Couchbase
>>> c = Couchbase.connect(bucket='default')
>>> c
<couchbase.connection.Connection bucket=default, nodes=['127.0.0.1:8091'] at 0xb21a50>
>>> c.set("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 import Couchbase
>>> c = Couchbase.connect(bucket='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.Couchbase
from twisted.internet import reactor
from txcouchbase.connection import Connection as TxCouchbase
cb = TxCouchbase(bucket='default')
def on_set(ret):
print "Set key. Result", ret
def on_get(ret):
print "Got key. Result", ret
reactor.stop()
cb.set("key", "value").addCallback(on_set)
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
The experimental_gevent_support constructor flag has now been removed. Instead, you can import the gcouchbase.connection package and use the GConnection class like so:
from gcouchbase.connection import GCouchbase
conn = GCouchbase(bucket='default')
print conn.set("foo", "bar")
print conn.get("foo")
The API functions exactly like the normal Connection API, except that the implementation is significantly different.
Note that the new GCouchbase class does not use the same implementation as the experimental support featured in 1.1.0
Other Examples
There are other examples in the examples directory.
Building documentaion
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
If you found an issue, please file it in our JIRA. You may also ask in the #libcouchbase IRC channel at freenode. (which is where the author(s) of this module may be found).
License
The Couchbase Python SDK is licensed under the Apache License 2.0.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for couchbase-1.2.3.win-amd64-py3.3.exe
Algorithm | Hash digest | |
---|---|---|
SHA256 | 45cb8c4533c08bedc588111383372b66733e1f93c045fe9fc405e0a52b0dd3dc |
|
MD5 | 28d51d7eeb05565219d106d3ecd3bc1a |
|
BLAKE2b-256 | 8471cf184070838da2d014614fbe83ec1b0dfcae414a4a5867575bc4e9e6bf32 |
Hashes for couchbase-1.2.3.win-amd64-py3.2.exe
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8c7604fbccab5e4de32a2709a35861411da1df1b886d0906dec6b417482a9aa |
|
MD5 | 2c4829d1d23eba0153927bcd4315d35d |
|
BLAKE2b-256 | 85bfa79c06afcb080fc08a90740da420cb141b89edb3f8b306fa7a3e39d22591 |
Hashes for couchbase-1.2.3.win-amd64-py2.7.exe
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6f71dc7f111fdeee312c6dc7fcb978c790bdb6ac2f7155fcdfd2d34e36266dde |
|
MD5 | f59d9d548bf4227915e5d14505dee545 |
|
BLAKE2b-256 | 33e76da6dfb3fee6bc3e3ac73dd96d2deb487c7c9c0b36e325a2c5114bc873b2 |
Hashes for couchbase-1.2.3.win-amd64-py2.6.exe
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3416055e3529e8f5af802486cadea6c64b3ac85f1da036bd3fbe7800a431bd5b |
|
MD5 | eee6290af2689f98f96cfac337097342 |
|
BLAKE2b-256 | af2dc7bd0d2347088a1b820df250d702f49a200f607fcffafd37fcd99cb64efd |