From d2fd6c112765eda4cc972e83308efc3808d70eac Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Wed, 19 Mar 2025 09:26:02 -0400 Subject: [PATCH 1/3] DOCSP-48170: Async examples for Serialization/Third Party Tools --- source/serialization.txt | 65 ++++++++++++++++++++++++++++++---------- source/tools.txt | 31 ++++++++++++++----- 2 files changed, 74 insertions(+), 22 deletions(-) diff --git a/source/serialization.txt b/source/serialization.txt index 723e7d5e..cfdaaf77 100644 --- a/source/serialization.txt +++ b/source/serialization.txt @@ -56,19 +56,40 @@ Serializing Custom Classes To serialize a custom class, you must convert the class to a dictionary. The following example serializes a custom class by using the ``vars()`` method, and then inserts the -serialized object into a collection: +serialized object into a collection. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` +tab to see the corresponding code: -.. code-block:: python +.. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. code-block:: python + + class Restaurant: + def __init__(self, name, cuisine): + self.name = name + self.cuisine = cuisine + + restaurant = Restaurant("Example Cafe", "Coffee") + restaurant_dict = vars(restaurant) + + collection.insert_one(restaurant_dict) + + .. tab:: Asynchronous + :tabid: async - class Restaurant: - def __init__(self, name, cuisine): - self.name = name - self.cuisine = cuisine + .. code-block:: python - restaurant = Restaurant("Example Cafe", "Coffee") - restaurant_dict = vars(restaurant) + class Restaurant: + def __init__(self, name, cuisine): + self.name = name + self.cuisine = cuisine + + restaurant = Restaurant("Example Cafe", "Coffee") + restaurant_dict = vars(restaurant) - collection.insert_one(restaurant_dict) + await collection.insert_one(restaurant_dict) The preceding example serializes the ``Restaurant`` object into the following dictionary: @@ -84,15 +105,29 @@ Deserializing Custom Classes To deserialize a custom class, you must convert the dictionary back into an instance of the class. The following example retrieves a document from a collection, and then converts -it back into a ``Restaurant`` object from the preceding example: +it back into a ``Restaurant`` object from the preceding example. Select the +:guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding code: + +.. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. code-block:: python + + def deserialize_restaurant(doc): + return Restaurant(name=doc["name"], cuisine=doc["cuisine"]) + + .. tab:: Asynchronous + :tabid: async -.. code-block:: python + .. code-block:: python - def deserialize_restaurant(doc): - return Restaurant(name=doc["name"], cuisine=doc["cuisine"]) + def deserialize_restaurant(doc): + return Restaurant(name=doc["name"], cuisine=doc["cuisine"]) - restaurant_doc = collection.find_one({"name": "Example Cafe"}) - restaurant = deserialize_restaurant(restaurant_doc) + restaurant_doc = await collection.find_one({"name": "Example Cafe"}) + restaurant = deserialize_restaurant(restaurant_doc) To learn more about retrieving documents from a collection, see the :ref:`pymongo-retrieve` guide. diff --git a/source/tools.txt b/source/tools.txt index de6eaa4d..7a1311e9 100644 --- a/source/tools.txt +++ b/source/tools.txt @@ -159,15 +159,32 @@ on `greenlets `__ instead of threads. To use gevent with {+driver-short+}, call gevent's ``monkey.patch_all()`` method *before* loading any other modules, as shown in the following -example: +example. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the +corresponding code: -.. code-block:: python +.. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. code-block:: python + + # You must call patch_all() *before* importing any other modules + from gevent import monkey + _ = monkey.patch_all() + from pymongo import MongoClient + client = MongoClient() + + .. tab:: Asynchronous + :tabid: async + + .. code-block:: python - # You must call patch_all() *before* importing any other modules - from gevent import monkey - _ = monkey.patch_all() - from pymongo import MongoClient - client = MongoClient() + # You must call patch_all() *before* importing any other modules + from gevent import monkey + _ = monkey.patch_all() + from pymongo import AsyncMongoClient + client = AsyncMongoClient() .. important:: Close MongoClient to Avoid Blocking From 69b377003139406315ab3122cc3f29ff9439c0ec Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Wed, 19 Mar 2025 09:33:36 -0400 Subject: [PATCH 2/3] Fix --- source/serialization.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/serialization.txt b/source/serialization.txt index cfdaaf77..f0b439ec 100644 --- a/source/serialization.txt +++ b/source/serialization.txt @@ -117,6 +117,9 @@ it back into a ``Restaurant`` object from the preceding example. Select the def deserialize_restaurant(doc): return Restaurant(name=doc["name"], cuisine=doc["cuisine"]) + + restaurant_doc = collection.find_one({"name": "Example Cafe"}) + restaurant = deserialize_restaurant(restaurant_doc) .. tab:: Asynchronous :tabid: async From cde58659e869bcf81c2f38d634d36755fc9d1654 Mon Sep 17 00:00:00 2001 From: Michael Morisi Date: Mon, 24 Mar 2025 10:28:09 -0400 Subject: [PATCH 3/3] NS feedback --- source/tools.txt | 31 +++++++------------------------ 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/source/tools.txt b/source/tools.txt index 7a1311e9..de6eaa4d 100644 --- a/source/tools.txt +++ b/source/tools.txt @@ -159,32 +159,15 @@ on `greenlets `__ instead of threads. To use gevent with {+driver-short+}, call gevent's ``monkey.patch_all()`` method *before* loading any other modules, as shown in the following -example. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the -corresponding code: +example: -.. tabs:: - - .. tab:: Synchronous - :tabid: sync - - .. code-block:: python - - # You must call patch_all() *before* importing any other modules - from gevent import monkey - _ = monkey.patch_all() - from pymongo import MongoClient - client = MongoClient() - - .. tab:: Asynchronous - :tabid: async - - .. code-block:: python +.. code-block:: python - # You must call patch_all() *before* importing any other modules - from gevent import monkey - _ = monkey.patch_all() - from pymongo import AsyncMongoClient - client = AsyncMongoClient() + # You must call patch_all() *before* importing any other modules + from gevent import monkey + _ = monkey.patch_all() + from pymongo import MongoClient + client = MongoClient() .. important:: Close MongoClient to Avoid Blocking