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)
| 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 |
no outgoing calls