OpenQuizz
Une application de gestion des contenus pédagogiques
CodecOptions Class Reference
Inheritance diagram for CodecOptions:
Collaboration diagram for CodecOptions:

Public Member Functions

def __new__ (cls, document_class=dict, tz_aware=False, uuid_representation=None, unicode_decode_error_handler="strict", tzinfo=None, type_registry=None)
 
def __repr__ (self)
 
def with_options (self, **kwargs)
 

Detailed Description

Encapsulates options used encoding and / or decoding BSON.

The `document_class` option is used to define a custom type for use
decoding BSON documents. Access to the underlying raw BSON bytes for
a document is available using the :class:`~bson.raw_bson.RawBSONDocument`
type::

  >>> from bson.raw_bson import RawBSONDocument
  >>> from bson.codec_options import CodecOptions
  >>> codec_options = CodecOptions(document_class=RawBSONDocument)
  >>> coll = db.get_collection('test', codec_options=codec_options)
  >>> doc = coll.find_one()
  >>> doc.raw
  '\\x16\\x00\\x00\\x00\\x07_id\\x00[0\\x165\\x91\\x10\\xea\\x14\\xe8\\xc5\\x8b\\x93\\x00'

The document class can be any type that inherits from
:class:`~collections.MutableMapping`::

  >>> class AttributeDict(dict):
  ...     # A dict that supports attribute access.
  ...     def __getattr__(self, key):
  ...         return self[key]
  ...     def __setattr__(self, key, value):
  ...         self[key] = value
  ...
  >>> codec_options = CodecOptions(document_class=AttributeDict)
  >>> coll = db.get_collection('test', codec_options=codec_options)
  >>> doc = coll.find_one()
  >>> doc._id
  ObjectId('5b3016359110ea14e8c58b93')

See :doc:`/examples/datetimes` for examples using the `tz_aware` and
`tzinfo` options.

See :class:`~bson.binary.UUIDLegacy` for examples using the
`uuid_representation` option.

:Parameters:
  - `document_class`: BSON documents returned in queries will be decoded
    to an instance of this class. Must be a subclass of
    :class:`~collections.MutableMapping`. Defaults to :class:`dict`.
  - `tz_aware`: If ``True``, BSON datetimes will be decoded to timezone
    aware instances of :class:`~datetime.datetime`. Otherwise they will be
    naive. Defaults to ``False``.
  - `uuid_representation`: The BSON representation to use when encoding
    and decoding instances of :class:`~uuid.UUID`. Defaults to
    :data:`~bson.binary.UuidRepresentation.PYTHON_LEGACY`. New
    applications should consider setting this to
    :data:`~bson.binary.UuidRepresentation.STANDARD` for cross language
    compatibility. See :ref:`handling-uuid-data-example` for details.
  - `unicode_decode_error_handler`: The error handler to apply when
    a Unicode-related error occurs during BSON decoding that would
    otherwise raise :exc:`UnicodeDecodeError`. Valid options include
    'strict', 'replace', and 'ignore'. Defaults to 'strict'.
  - `tzinfo`: A :class:`~datetime.tzinfo` subclass that specifies the
    timezone to/from which :class:`~datetime.datetime` objects should be
    encoded/decoded.
  - `type_registry`: Instance of :class:`TypeRegistry` used to customize
    encoding and decoding behavior.

.. versionadded:: 3.8
   `type_registry` attribute.

.. warning:: Care must be taken when changing
   `unicode_decode_error_handler` from its default value ('strict').
   The 'replace' and 'ignore' modes should not be used when documents
   retrieved from the server will be modified in the client application
   and stored back to the server.

Member Function Documentation

◆ __new__()

def __new__ (   cls,
  document_class = dict,
  tz_aware = False,
  uuid_representation = None,
  unicode_decode_error_handler = "strict",
  tzinfo = None,
  type_registry = None 
)

◆ __repr__()

def __repr__ (   self)

◆ with_options()

def with_options (   self,
**  kwargs 
)
Make a copy of this CodecOptions, overriding some options::

    >>> from bson.codec_options import DEFAULT_CODEC_OPTIONS
    >>> DEFAULT_CODEC_OPTIONS.tz_aware
    False
    >>> options = DEFAULT_CODEC_OPTIONS.with_options(tz_aware=True)
    >>> options.tz_aware
    True

.. versionadded:: 3.5

Reimplemented in JSONOptions.


The documentation for this class was generated from the following file: