@@ -40,7 +40,7 @@ to save an instance of ``Decimal`` with {+driver-short+} results in an
4040 from decimal import Decimal
4141
4242 num = Decimal("45.321")
43- db. coll.insert_one({"num": num})
43+ db[" coll"] .insert_one({"num": num})
4444
4545 .. output::
4646 :language: shell
@@ -56,8 +56,8 @@ Define a Type Codec Class
5656~~~~~~~~~~~~~~~~~~~~~~~~~
5757
5858To encode a custom type, you must first define a **type codec.** A type codec
59- describes how an instance of a custom type can be
60- converted to and from one of the types the ``bson`` module can already encode.
59+ describes how an instance of a custom type is
60+ converted to and from a type that the ``bson`` module can already encode.
6161
6262When you define a type codec, your class must inherit from one of the base classes in the
6363``codec_options`` module. The following table describes these base classes, and when
@@ -75,30 +75,30 @@ and how to implement them:
7575 * - ``codec_options.TypeEncoder``
7676 - Inherit from this class to define a codec that
7777 encodes a custom Python type to a known BSON type.
78- - - ``python_type`` attribute: The Python type acted on by this type codec
78+ - - ``python_type`` attribute: The custom Python type that's encoded by this type codec
7979 - ``transform_python()`` method: Function that transforms a custom type value into a type
8080 that BSON can encode
8181
8282 * - ``codec_options.TypeDecoder``
8383 - Inherit from this class to define a codec that
8484 decodes a specified BSON type into a custom Python type.
85- - - ``bson_type`` attribute: The BSON type acted on by this type codec
85+ - - ``bson_type`` attribute: The BSON type that's decoded by this type codec
8686 - ``transform_bson()`` method: Function that transforms a standard BSON type value
8787 into the custom type
8888
8989 * - ``codec_options.TypeCodec``
9090 - Inherit from this class to define a codec that
9191 can both encode and decode a custom type.
92- - - ``python_type`` attribute: The Python type acted on by this type codec
93- - ``bson_type`` attribute: The BSON type acted on by this type codec
92+ - - ``python_type`` attribute: The custom Python type that's encoded by this type codec
93+ - ``bson_type`` attribute: The BSON type that's decoded by this type codec
9494 - ``transform_bson()`` method: Function that transforms a standard BSON type value
9595 into the custom type
9696 - ``transform_python()`` method: Function that transforms a custom type value into a type
9797 that BSON can encode
9898
9999Because the example ``Decimal`` custom type can be converted to and from a
100100``Decimal128`` instance, you must define how to encode and decode this type.
101- Therefore, the``Decimal`` type codec class must inherit from
101+ Therefore, the ``Decimal`` type codec class must inherit from
102102the ``TypeCodec`` base class:
103103
104104.. code-block:: python
@@ -125,6 +125,9 @@ To do so, create an instance of the ``TypeRegistry`` class, passing in an instan
125125of your type codec class inside a list. If you create multiple custom codecs, you can
126126pass them all to the ``TypeRegistry`` constructor.
127127
128+ The following code examples adds an instance of the ``DecimalCodec`` type codec to
129+ the type registry:
130+
128131.. code-block:: python
129132
130133 from bson.codec_options import TypeRegistry
@@ -196,8 +199,8 @@ Encode a Subtype
196199----------------
197200
198201You might also need to encode one or more types that inherit from your custom type.
199- Consider this subtype of the ``Decimal`` type , which contains a method to return its value
200- as an integer:
202+ Consider the following subtype of the ``Decimal`` class , which contains a method to
203+ return its value as an integer:
201204
202205.. code-block:: python
203206
@@ -225,14 +228,15 @@ codec for it, {+driver-short+} raises an error:
225228 of type: <class 'decimal.Decimal'>
226229
227230To encode an instance of the ``DecimalInt`` class, you must define a type codec for
228- the class. This type codec must inherit from the parent class's codec:
231+ the class. This type codec must inherit from the parent class's codec, ``DecimalCodec``,
232+ as shown in the following example:
229233
230234.. code-block:: python
231235
232236 class DecimalIntCodec(DecimalCodec):
233237 @property
234238 def python_type(self):
235- # The Python type acted upon by this type codec
239+ # The Python type encoded by this type codec
236240 return DecimalInt
237241
238242You can then add the sublcass's type codec to the type registry and encode instances
@@ -252,7 +256,6 @@ of the custom type:
252256 codec_options = CodecOptions(type_registry=type_registry)
253257
254258 collection = db.get_collection("test", codec_options=codec_options)
255- collection.drop()
256259 collection.insert_one({"num": DecimalInt("45.321")})
257260
258261 my_doc = collection.find_one()
@@ -273,7 +276,7 @@ Define a Fallback Encoder
273276
274277You can also register a **fallback encoder**, a callable to encode types not recognized
275278by BSON and for which no type codec has been registered.
276- Like the ``transform_python()`` method, the fallback encoder accepts an unencodable
279+ The fallback encoder accepts an unencodable
277280value as a parameter and returns a BSON-encodable value.
278281
279282The following fallback encoder encodes Python's ``Decimal`` type to a ``Decimal128``:
@@ -332,9 +335,10 @@ class:
332335
333336Encode Unknown Types
334337~~~~~~~~~~~~~~~~~~~~
338+ .. TODO: consider new example
335339
336340Because fallback encoders don't need to declare the types that they encode
337- beforehand, you can them in cases where a ``TypeEncoder`` doesn't work.
341+ beforehand, you can use them in cases where a ``TypeEncoder`` doesn't work.
338342For example, you can use a fallback encoder to save arbitrary objects to MongoDB.
339343Consider the following arbitrary custom types:
340344
0 commit comments