@@ -342,27 +342,27 @@ Context managers
342342Now that we've gone through the basics, let's take a look at how to simplify
343343the process of connection, database acquisition, and document management
344344through the use of Python *with * blocks and this library's context managers.
345- Handling your business using *with * blocks saves you from having to connect and
345+
346+ Handling your business using *with * blocks saves you from having to connect and
346347disconnect your client as well as saves you from having to perform a lot of
347348fetch and save operations as the context managers handle these operations for
348- you. This example uses the ``cloudant `` context helper to illustrate the
349+ you.
350+
351+ This example uses the ``cloudant `` context helper to illustrate the
349352process but identical functionality exists for CouchDB through the ``couchdb ``
350353and ``couchdb_admin_party `` context helpers.
351354
352355.. code-block :: python
353356
354- # cloudant context helper
355357 from cloudant import cloudant
356358
357- # couchdb context helper
359+ # ...or use CouchDB variant
358360 # from cloudant import couchdb
359361
360- from cloudant.document import Document
361-
362362 # Perform a connect upon entry and a disconnect upon exit of the block
363363 with cloudant(USERNAME , PASSWORD , account = ACCOUNT_NAME ) as client:
364364
365- # CouchDB variant
365+ # ...or use CouchDB variant
366366 # with couchdb(USERNAME, PASSWORD, url=COUCHDB_URL) as client:
367367
368368 # Perform client tasks...
@@ -378,9 +378,22 @@ and ``couchdb_admin_party`` context helpers.
378378 # You can open an existing database
379379 del my_database
380380 my_database = client[' my_database' ]
381-
381+
382+ The following example uses the ``Document `` context manager. Here we make
383+ multiple updates to a single document. Note that we don't save to the server
384+ after each update. We only save once to the server upon exiting the ``Document ``
385+ context manager.
386+
387+ .. code-block :: python
388+
389+ from cloudant import cloudant
390+ from cloudant.document import Document
391+
392+ with cloudant(USERNAME , PASSWORD , account = ACCOUNT_NAME ) as client:
393+
394+ my_database = client.create_database(' my_database' )
395+
382396 # Performs a fetch upon entry and a save upon exit of this block
383- # Use this context manager to create or update a Document
384397 with Document(my_database, ' julia30' ) as doc:
385398 doc[' name' ] = ' Julia'
386399 doc[' age' ] = 30
@@ -394,6 +407,22 @@ and ``couchdb_admin_party`` context helpers.
394407
395408 print (' Databases: {0} ' .format(client.all_dbs()))
396409
410+ Always use the ``_deleted `` document property to delete a document from within
411+ a ``Document `` context manager. For example:
412+
413+ .. code-block :: python
414+
415+ with Document(my_database, ' julia30' ) as doc:
416+ doc[' _deleted' ] = True
417+
418+ *You can also delete non underscore prefixed document keys to reduce the size of the request. *
419+
420+ .. warning :: Don't use the ``doc.delete()`` method inside your ``Document``
421+ context manager. This method immediately deletes the document on
422+ the server and clears the local document dictionary. A new, empty
423+ document is still saved to the server upon exiting the context
424+ manager.
425+
397426****************
398427Endpoint access
399428****************
0 commit comments