Skip to content

Commit c51b90a

Browse files
committed
fixes
1 parent 770e501 commit c51b90a

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

source/data-formats/custom-types.txt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ Custom Types
1919
Overview
2020
--------
2121

22-
This guide explains how to create a custom type with {+driver-short+}.
23-
To create a custom type, define a class that inherits from ``codec_options.TypeCodec``.
24-
You can use this *type codec* to populate an instance of ``codec_options.TypeRegistry``,
25-
a type registry. You can then use the type registry to create a custom-type-aware
26-
``Collection`` object.
22+
This guide explains how to use {+driver-short+} to encode and decode custom types.
2723

2824
Encode a Custom Type
2925
--------------------
@@ -36,6 +32,7 @@ to save an instance of ``Decimal`` with {+driver-short+} results in an
3632
``InvalidDocument`` exception, as shown in the following code example:
3733

3834
.. io-code-block::
35+
:copyable: true
3936

4037
.. input::
4138
:language: python
@@ -45,8 +42,6 @@ to save an instance of ``Decimal`` with {+driver-short+} results in an
4542
num = Decimal("45.321")
4643
db.coll.insert_one({"num": num})
4744

48-
<input code>
49-
5045
.. output::
5146
:language: shell
5247

@@ -127,7 +122,8 @@ Add Codec to the Type Registry
127122
After defining a custom type codec, you must add it to {+driver-short+}'s **type registry**,
128123
the list of types the driver can encode and decode.
129124
To do so, create an instance of the ``TypeRegistry`` class, passing in an instance
130-
of your type codec class inside a list:
125+
of your type codec class inside a list. If you create multiple custom codecs, you can
126+
pass them all to the ``TypeRegistry`` constructor.
131127

132128
.. code-block:: python
133129

@@ -136,9 +132,6 @@ of your type codec class inside a list:
136132
decimal_codec = DecimalCodec()
137133
type_registry = TypeRegistry([decimal_codec])
138134

139-
If you create multiple custom codecs, you can pass them all to the ``TypeRegistry``
140-
constructor.
141-
142135
.. note::
143136

144137
Once instantiated, registries are immutable and the only way to add codecs
@@ -161,6 +154,7 @@ object as a keyword argument. Pass your ``CodecOptions`` object to the
161154
You can then encode and decode instances of the ``Decimal`` class:
162155

163156
.. io-code-block::
157+
:copyable: true
164158

165159
.. input::
166160
:language: python
@@ -183,6 +177,7 @@ that {+driver-short+} stores an instance of the ``Decimal`` class as a ``Decimal
183177
value:
184178

185179
.. io-code-block::
180+
:copyable: true
186181

187182
.. input::
188183
:language: python
@@ -214,6 +209,7 @@ If you try to save an instance of the ``DecimalInt`` class without first registe
214209
codec for it, {+driver-short+} raises an error:
215210

216211
.. io-code-block::
212+
:copyable: true
217213

218214
.. input::
219215
:language: python
@@ -243,6 +239,7 @@ You can then add the sublcass's type codec to the type registry and encode insta
243239
of the custom type:
244240

245241
.. io-code-block::
242+
:copyable: true
246243

247244
.. input::
248245
:language: python
@@ -309,6 +306,7 @@ You can then use this reference to a collection to store instances of the ``Deci
309306
class:
310307

311308
.. io-code-block::
309+
:copyable: true
312310

313311
.. input::
314312
:language: python
@@ -384,6 +382,7 @@ You can then add the ``PickledBinaryDecoder`` to the codec options for a collect
384382
and use it to encode and decode your custom types:
385383

386384
.. io-code-block::
385+
:copyable: true
387386

388387
.. input::
389388
:language: python
@@ -403,7 +402,7 @@ and use it to encode and decode your custom types:
403402
my_doc = collection.find_one()
404403

405404
print(isinstance(my_doc["str"], MyStringType))
406-
assert(isinstance(my_doc["num"], MyNumberType))
405+
print(isinstance(my_doc["num"], MyNumberType))
407406

408407
.. output::
409408
:language: shell

0 commit comments

Comments
 (0)