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

Method as_uuid

bson/binary.py:378–420  ·  view source on GitHub ↗

Create a Python UUID from this BSON Binary object. Decodes this binary object as a native :class:`uuid.UUID` instance with the provided ``uuid_representation``. Raises :exc:`ValueError` if this :class:`~bson.binary.Binary` instance does not contain a UUID.

(self, uuid_representation: int = UuidRepresentation.STANDARD)

Source from the content-addressed store, hash-verified

376 return cls(payload, subtype)
377
378 def as_uuid(self, uuid_representation: int = UuidRepresentation.STANDARD) -> UUID:
379 """Create a Python UUID from this BSON Binary object.
380
381 Decodes this binary object as a native :class:`uuid.UUID` instance
382 with the provided ``uuid_representation``.
383
384 Raises :exc:`ValueError` if this :class:`~bson.binary.Binary` instance
385 does not contain a UUID.
386
387 :param uuid_representation: A member of
388 :class:`~bson.binary.UuidRepresentation`. Default:
389 :const:`~bson.binary.UuidRepresentation.STANDARD`.
390 See `UUID representations <https://www.mongodb.com/docs/languages/python/pymongo-driver/current/data-formats/uuid/#universally-unique-ids--uuids->`_ for details.
391
392 .. versionadded:: 3.11
393 """
394 if self.subtype not in ALL_UUID_SUBTYPES:
395 raise ValueError(f"cannot decode subtype {self.subtype} as a uuid")
396
397 if uuid_representation not in ALL_UUID_REPRESENTATIONS:
398 raise ValueError(
399 "uuid_representation must be a value from bson.binary.UuidRepresentation"
400 )
401
402 if uuid_representation == UuidRepresentation.UNSPECIFIED:
403 raise ValueError("uuid_representation cannot be UNSPECIFIED")
404 elif uuid_representation == UuidRepresentation.PYTHON_LEGACY:
405 if self.subtype == OLD_UUID_SUBTYPE:
406 return UUID(bytes=self)
407 elif uuid_representation == UuidRepresentation.JAVA_LEGACY:
408 if self.subtype == OLD_UUID_SUBTYPE:
409 return UUID(bytes=self[0:8][::-1] + self[8:16][::-1])
410 elif uuid_representation == UuidRepresentation.CSHARP_LEGACY:
411 if self.subtype == OLD_UUID_SUBTYPE:
412 return UUID(bytes_le=self)
413 else:
414 # uuid_representation == UuidRepresentation.STANDARD
415 if self.subtype == UUID_SUBTYPE:
416 return UUID(bytes=self)
417
418 raise ValueError(
419 f"cannot decode subtype {self.subtype} to {UUID_REPRESENTATION_NAMES[uuid_representation]}"
420 )
421
422 @classmethod
423 @overload

Callers 9

test_decoding_1Method · 0.95
_test_decoding_legacyMethod · 0.95
_get_binaryFunction · 0.95
_binary_or_uuidFunction · 0.95
test_uuid_legacyMethod · 0.80
test_encrypt_decryptMethod · 0.80
run_test_casesMethod · 0.80
test_encrypt_decryptMethod · 0.80
run_test_casesMethod · 0.80

Calls

no outgoing calls

Tested by 7

test_decoding_1Method · 0.76
_test_decoding_legacyMethod · 0.76
test_uuid_legacyMethod · 0.64
test_encrypt_decryptMethod · 0.64
run_test_casesMethod · 0.64
test_encrypt_decryptMethod · 0.64
run_test_casesMethod · 0.64