MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / Binary

Class Binary

bson/binary.py:273–665  ·  view source on GitHub ↗

Representation of BSON binary data. We want to represent Python strings as the BSON string type. We need to wrap binary data so that we can tell the difference between what should be considered binary data and what should be considered a string when we encode to BSON. Subtype 9

Source from the content-addressed store, hash-verified

271
272
273class Binary(bytes):
274 """Representation of BSON binary data.
275
276 We want to represent Python strings as the BSON string type.
277 We need to wrap binary data so that we can tell
278 the difference between what should be considered binary data and
279 what should be considered a string when we encode to BSON.
280
281 Subtype 9 provides a space-efficient representation of 1-dimensional vector data.
282 Its data is prepended with two bytes of metadata.
283 The first (dtype) describes its data type, such as float32 or int8.
284 The second (padding) prescribes the number of bits to ignore in the final byte.
285 This is relevant when the element size of the dtype is not a multiple of 8.
286
287 Raises TypeError if `subtype` is not an instance of :class:`int`.
288 Raises ValueError if `subtype` is not in [0, 256).
289
290 .. note::
291 Instances of Binary with subtype 0 will be decoded directly to :class:`bytes`.
292
293 :param data: the binary data to represent. Can be any bytes-like type
294 that implements the buffer protocol.
295 :param subtype: the `binary subtype
296 <https://bsonspec.org/spec.html>`_
297 to use
298
299 .. versionchanged:: 3.9
300 Support any bytes-like type that implements the buffer protocol.
301
302 .. versionchanged:: 4.10
303 Addition of vector subtype.
304 """
305
306 _type_marker = 5
307 __subtype: int
308
309 def __new__(
310 cls: Type[Binary],
311 data: Union[memoryview, bytes, bytearray, _mmap, _array[Any]],
312 subtype: int = BINARY_SUBTYPE,
313 ) -> Binary:
314 if not isinstance(subtype, int):
315 raise TypeError(f"subtype must be an instance of int, not {type(subtype)}")
316 if subtype >= 256 or subtype < 0:
317 raise ValueError("subtype must be contained in [0, 256)")
318 # Support any type that implements the buffer protocol.
319 self = bytes.__new__(cls, memoryview(data).tobytes())
320 self.__subtype = subtype
321 return self
322
323 @classmethod
324 def from_uuid(
325 cls: Type[Binary], uuid: UUID, uuid_representation: int = UuidRepresentation.STANDARD
326 ) -> Binary:
327 """Create a BSON Binary object from a Python UUID.
328
329 Creates a :class:`~bson.binary.Binary` object from a
330 :class:`uuid.UUID` instance. Assumes that the native

Callers 15

test_uuid_defaultMethod · 0.90
test_binaryMethod · 0.90
test_corrupt_chunkMethod · 0.90
test_corrupt_chunkMethod · 0.90
test_basic_encodeMethod · 0.90
test_vectorMethod · 0.90

Calls

no outgoing calls

Tested by 15

test_uuid_defaultMethod · 0.72
test_binaryMethod · 0.72
test_corrupt_chunkMethod · 0.72
test_corrupt_chunkMethod · 0.72
test_basic_encodeMethod · 0.72
test_vectorMethod · 0.72