Skip to content

Commit d2fd6c1

Browse files
committed
DOCSP-48170: Async examples for Serialization/Third Party Tools
1 parent a235231 commit d2fd6c1

File tree

2 files changed

+74
-22
lines changed

2 files changed

+74
-22
lines changed

source/serialization.txt

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,40 @@ Serializing Custom Classes
5656

5757
To serialize a custom class, you must convert the class to a dictionary. The following
5858
example serializes a custom class by using the ``vars()`` method, and then inserts the
59-
serialized object into a collection:
59+
serialized object into a collection. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous`
60+
tab to see the corresponding code:
6061

61-
.. code-block:: python
62+
.. tabs::
63+
64+
.. tab:: Synchronous
65+
:tabid: sync
66+
67+
.. code-block:: python
68+
69+
class Restaurant:
70+
def __init__(self, name, cuisine):
71+
self.name = name
72+
self.cuisine = cuisine
73+
74+
restaurant = Restaurant("Example Cafe", "Coffee")
75+
restaurant_dict = vars(restaurant)
76+
77+
collection.insert_one(restaurant_dict)
78+
79+
.. tab:: Asynchronous
80+
:tabid: async
6281

63-
class Restaurant:
64-
def __init__(self, name, cuisine):
65-
self.name = name
66-
self.cuisine = cuisine
82+
.. code-block:: python
6783

68-
restaurant = Restaurant("Example Cafe", "Coffee")
69-
restaurant_dict = vars(restaurant)
84+
class Restaurant:
85+
def __init__(self, name, cuisine):
86+
self.name = name
87+
self.cuisine = cuisine
88+
89+
restaurant = Restaurant("Example Cafe", "Coffee")
90+
restaurant_dict = vars(restaurant)
7091

71-
collection.insert_one(restaurant_dict)
92+
await collection.insert_one(restaurant_dict)
7293

7394
The preceding example serializes the ``Restaurant`` object into the following dictionary:
7495

@@ -84,15 +105,29 @@ Deserializing Custom Classes
84105

85106
To deserialize a custom class, you must convert the dictionary back into an instance of
86107
the class. The following example retrieves a document from a collection, and then converts
87-
it back into a ``Restaurant`` object from the preceding example:
108+
it back into a ``Restaurant`` object from the preceding example. Select the
109+
:guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding code:
110+
111+
.. tabs::
112+
113+
.. tab:: Synchronous
114+
:tabid: sync
115+
116+
.. code-block:: python
117+
118+
def deserialize_restaurant(doc):
119+
return Restaurant(name=doc["name"], cuisine=doc["cuisine"])
120+
121+
.. tab:: Asynchronous
122+
:tabid: async
88123

89-
.. code-block:: python
124+
.. code-block:: python
90125

91-
def deserialize_restaurant(doc):
92-
return Restaurant(name=doc["name"], cuisine=doc["cuisine"])
126+
def deserialize_restaurant(doc):
127+
return Restaurant(name=doc["name"], cuisine=doc["cuisine"])
93128

94-
restaurant_doc = collection.find_one({"name": "Example Cafe"})
95-
restaurant = deserialize_restaurant(restaurant_doc)
129+
restaurant_doc = await collection.find_one({"name": "Example Cafe"})
130+
restaurant = deserialize_restaurant(restaurant_doc)
96131

97132
To learn more about retrieving documents from a collection, see the :ref:`pymongo-retrieve`
98133
guide.

source/tools.txt

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,32 @@ on `greenlets <https://pypi.org/project/greenlet/>`__ instead of threads.
159159

160160
To use gevent with {+driver-short+}, call gevent's
161161
``monkey.patch_all()`` method *before* loading any other modules, as shown in the following
162-
example:
162+
example. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the
163+
corresponding code:
163164

164-
.. code-block:: python
165+
.. tabs::
166+
167+
.. tab:: Synchronous
168+
:tabid: sync
169+
170+
.. code-block:: python
171+
172+
# You must call patch_all() *before* importing any other modules
173+
from gevent import monkey
174+
_ = monkey.patch_all()
175+
from pymongo import MongoClient
176+
client = MongoClient()
177+
178+
.. tab:: Asynchronous
179+
:tabid: async
180+
181+
.. code-block:: python
165182

166-
# You must call patch_all() *before* importing any other modules
167-
from gevent import monkey
168-
_ = monkey.patch_all()
169-
from pymongo import MongoClient
170-
client = MongoClient()
183+
# You must call patch_all() *before* importing any other modules
184+
from gevent import monkey
185+
_ = monkey.patch_all()
186+
from pymongo import AsyncMongoClient
187+
client = AsyncMongoClient()
171188

172189
.. important:: Close MongoClient to Avoid Blocking
173190

0 commit comments

Comments
 (0)