Blog how to use Python SDK API's with Couchbase

Hi CB team ,
I have some suggestion here . We are trying to CB to migrate our DB from Oracle and application to use CB . We are using Python SDK to get our query into GraphQL schema and finally to UI .
While using python SDK 2.5.1 API and its related method I found its pretty difficult to mind map on few things . how to use get , get_multi , retrieve_multi, subdocument SD and lookup_in methods . It seems it is challenging o return value of those methods as each methods are different and writing python code I feel its hard to know how to transform some method return like N1QLResuest to List or Tuple or dictionary and dictionary items . Its being hard to get around those python code because there is no clear documentation (even on the API) how can I use different objects .

Giving you a example :

   rv = cb.get("key_1")     # cb is bucket 
    # rv.value  is of <class 'dict'> ,  [rv.value] is of <class 'list'  , rv.value.items() is of <class 'dict_items'>
    # can't use rv.value.items() as it is not JSON serializable

as you different rv. returns different results and we have no document how could we use what items

You guys usually do a very good blogs and documentation. I think I didn’t find encircling on the topic how I can achieve the results whatever Python Array format I want and finally any kind of JSON objects .
It would be great help if CB Python SDK team provide some guidelines and write a blog on that .

Thanks for your help .

Hi, there is some documentation here about return values:

https://docs.couchbase.com/sdk-api/couchbase-python-client-2.5.6/api/results.html

I’ll create a JIRA to write a blog on this topic , but the PyDoc/Sphinx API documentation embedded in the code should also mention what sort of result object is returned by each call.

In the example you give, the dict contained in rv.value should be analogous to JSON, i.e. directly serialisable to JSON via json.dump[s].
For multiresults from e.g. get_multi, it is a little more complicated if you wish to serialise the result, as multiresult is effectively a dict of Result objects mapped by key. So, if you wanted to serialise a multiresult as a large JSON object, you could do:

json.dumps({key: result.value for key, result in rv.items()})

Subdoc results as returned by lookup_in are documented here:

https://docs.couchbase.com/sdk-api/couchbase-python-client-2.5.6/api/subdoc.html#couchbase.result.SubdocResult

We’ll look at adding some more pointers/tutorials about result objects to make all this clearer.

In the upcoming version 3, we have included extensive type annotations to make this clearer to the end user and IDEs.

Hope that helps,

Ellis

@ellis.breen thanks for pointing out with API doc for 2.5.6 . Until now I was referring 2.5.1 API doc . In theory there is no big difference but I will keep this for easy reference. Since JSON is a unique and widespread nomenclature , serialization to JSON from any other dictionary type is going to be very helpful for storing , retrieving the data which supports JSON as well so blog around it is of great help for the Future application developers .
For get_multi yes I have to eventually use your method to get the right context in serialize way for my application to understand .
Excited to see upcoming API 3.0 release .
thanks again.