@@ -306,24 +306,50 @@ The following code example shows this process:
306
306
collection = db.get_collection("test", codec_options=codec_options)
307
307
308
308
You can then use this reference to a collection to store instances of the ``Decimal``
309
- class:
309
+ class. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the
310
+ corresponding code.
310
311
311
- .. io-code-block::
312
- :copyable: true
312
+ .. tabs::
313
+
314
+ .. tab:: Synchronous
315
+ :tabid: sync
313
316
314
- .. input ::
315
- :language: python
317
+ .. io-code-block ::
318
+ :copyable: true
316
319
317
- import pprint
320
+ .. input::
321
+ :language: python
318
322
319
- collection.insert_one({"num": Decimal("45.321")})
320
- my_doc = collection.find_one()
321
- pprint.pprint(my_doc)
323
+ import pprint
322
324
323
- .. output::
324
- :language: shell
325
+ collection.insert_one({"num": Decimal("45.321")})
326
+ my_doc = collection.find_one()
327
+ pprint.pprint(my_doc)
325
328
326
- {'_id': ObjectId('...'), 'num': Decimal128('45.321')}
329
+ .. output::
330
+ :language: shell
331
+
332
+ {'_id': ObjectId('...'), 'num': Decimal128('45.321')}
333
+
334
+ .. tab:: Asynchronous
335
+ :tabid: async
336
+
337
+ .. io-code-block::
338
+ :copyable: true
339
+
340
+ .. input::
341
+ :language: python
342
+
343
+ import pprint
344
+
345
+ await collection.insert_one({"num": Decimal("45.321")})
346
+ my_doc = await collection.find_one()
347
+ pprint.pprint(my_doc)
348
+
349
+ .. output::
350
+ :language: shell
351
+
352
+ {'_id': ObjectId('...'), 'num': Decimal128('45.321')}
327
353
328
354
.. note::
329
355
@@ -383,36 +409,74 @@ back into Python objects:
383
409
return value
384
410
385
411
You can then add the ``PickledBinaryDecoder`` to the codec options for a collection
386
- and use it to encode and decode your custom types:
412
+ and use it to encode and decode your custom types. Select the :guilabel:`Synchronous` or
413
+ :guilabel:`Asynchronous` tab to see the corresponding code.
387
414
388
- .. io-code-block::
389
- :copyable: true
415
+ .. tabs::
416
+
417
+ .. tab:: Synchronous
418
+ :tabid: sync
390
419
391
- .. input ::
392
- :language: python
420
+ .. io-code-block ::
421
+ :copyable: true
393
422
394
- from bson.codec_options import CodecOptions,TypeRegistry
423
+ .. input::
424
+ :language: python
395
425
396
- codec_options = CodecOptions(
397
- type_registry=TypeRegistry(
398
- [PickledBinaryDecoder()], fallback_encoder=fallback_pickle_encoder
399
- )
400
- )
426
+ from bson.codec_options import CodecOptions,TypeRegistry
401
427
402
- collection = db.get_collection("test", codec_options=codec_options)
403
- collection.insert_one (
404
- {"_id": 1, "str": MyStringType("hello world"), "num": MyNumberType(2)}
405
- )
406
- my_doc = collection.find_one( )
428
+ codec_options = CodecOptions(
429
+ type_registry=TypeRegistry (
430
+ [PickledBinaryDecoder()], fallback_encoder=fallback_pickle_encoder
431
+ )
432
+ )
407
433
408
- print(isinstance(my_doc["str"], MyStringType))
409
- print(isinstance(my_doc["num"], MyNumberType))
434
+ collection = db.get_collection("test", codec_options=codec_options)
435
+ collection.insert_one(
436
+ {"_id": 1, "str": MyStringType("hello world"), "num": MyNumberType(2)}
437
+ )
438
+ my_doc = collection.find_one()
410
439
411
- .. output::
412
- :language: shell
440
+ print(isinstance(my_doc["str"], MyStringType))
441
+ print(isinstance(my_doc["num"], MyNumberType))
442
+
443
+ .. output::
444
+ :language: shell
445
+
446
+ True
447
+ True
448
+
449
+ .. tab:: Asynchronous
450
+ :tabid: async
451
+
452
+ .. io-code-block::
453
+ :copyable: true
454
+
455
+ .. input::
456
+ :language: python
457
+
458
+ from bson.codec_options import CodecOptions,TypeRegistry
459
+
460
+ codec_options = CodecOptions(
461
+ type_registry=TypeRegistry(
462
+ [PickledBinaryDecoder()], fallback_encoder=fallback_pickle_encoder
463
+ )
464
+ )
465
+
466
+ collection = db.get_collection("test", codec_options=codec_options)
467
+ await collection.insert_one(
468
+ {"_id": 1, "str": MyStringType("hello world"), "num": MyNumberType(2)}
469
+ )
470
+ my_doc = await collection.find_one()
471
+
472
+ print(isinstance(my_doc["str"], MyStringType))
473
+ print(isinstance(my_doc["num"], MyNumberType))
474
+
475
+ .. output::
476
+ :language: shell
413
477
414
- True
415
- True
478
+ True
479
+ True
416
480
417
481
Limitations
418
482
-----------
0 commit comments