@@ -19,11 +19,7 @@ Custom Types
19
19
Overview
20
20
--------
21
21
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.
27
23
28
24
Encode a Custom Type
29
25
--------------------
@@ -36,6 +32,7 @@ to save an instance of ``Decimal`` with {+driver-short+} results in an
36
32
``InvalidDocument`` exception, as shown in the following code example:
37
33
38
34
.. io-code-block::
35
+ :copyable: true
39
36
40
37
.. input::
41
38
:language: python
@@ -45,8 +42,6 @@ to save an instance of ``Decimal`` with {+driver-short+} results in an
45
42
num = Decimal("45.321")
46
43
db.coll.insert_one({"num": num})
47
44
48
- <input code>
49
-
50
45
.. output::
51
46
:language: shell
52
47
@@ -127,7 +122,8 @@ Add Codec to the Type Registry
127
122
After defining a custom type codec, you must add it to {+driver-short+}'s **type registry**,
128
123
the list of types the driver can encode and decode.
129
124
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.
131
127
132
128
.. code-block:: python
133
129
@@ -136,9 +132,6 @@ of your type codec class inside a list:
136
132
decimal_codec = DecimalCodec()
137
133
type_registry = TypeRegistry([decimal_codec])
138
134
139
- If you create multiple custom codecs, you can pass them all to the ``TypeRegistry``
140
- constructor.
141
-
142
135
.. note::
143
136
144
137
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
161
154
You can then encode and decode instances of the ``Decimal`` class:
162
155
163
156
.. io-code-block::
157
+ :copyable: true
164
158
165
159
.. input::
166
160
:language: python
@@ -183,6 +177,7 @@ that {+driver-short+} stores an instance of the ``Decimal`` class as a ``Decimal
183
177
value:
184
178
185
179
.. io-code-block::
180
+ :copyable: true
186
181
187
182
.. input::
188
183
:language: python
@@ -214,6 +209,7 @@ If you try to save an instance of the ``DecimalInt`` class without first registe
214
209
codec for it, {+driver-short+} raises an error:
215
210
216
211
.. io-code-block::
212
+ :copyable: true
217
213
218
214
.. input::
219
215
:language: python
@@ -243,6 +239,7 @@ You can then add the sublcass's type codec to the type registry and encode insta
243
239
of the custom type:
244
240
245
241
.. io-code-block::
242
+ :copyable: true
246
243
247
244
.. input::
248
245
:language: python
@@ -309,6 +306,7 @@ You can then use this reference to a collection to store instances of the ``Deci
309
306
class:
310
307
311
308
.. io-code-block::
309
+ :copyable: true
312
310
313
311
.. input::
314
312
:language: python
@@ -384,6 +382,7 @@ You can then add the ``PickledBinaryDecoder`` to the codec options for a collect
384
382
and use it to encode and decode your custom types:
385
383
386
384
.. io-code-block::
385
+ :copyable: true
387
386
388
387
.. input::
389
388
:language: python
@@ -403,7 +402,7 @@ and use it to encode and decode your custom types:
403
402
my_doc = collection.find_one()
404
403
405
404
print(isinstance(my_doc["str"], MyStringType))
406
- assert (isinstance(my_doc["num"], MyNumberType))
405
+ print (isinstance(my_doc["num"], MyNumberType))
407
406
408
407
.. output::
409
408
:language: shell
0 commit comments