Create a BSON Binary object from a Python UUID. Creates a :class:`~bson.binary.Binary` object from a :class:`uuid.UUID` instance. Assumes that the native :class:`uuid.UUID` instance uses the byte-order implied by the provided ``uuid_representation``. Raises
(
cls: Type[Binary], uuid: UUID, uuid_representation: int = UuidRepresentation.STANDARD
)
| 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 |
| 331 | :class:`uuid.UUID` instance uses the byte-order implied by the |
| 332 | provided ``uuid_representation``. |
| 333 | |
| 334 | Raises :exc:`TypeError` if `uuid` is not an instance of |
| 335 | :class:`~uuid.UUID`. |
| 336 | |
| 337 | :param uuid: A :class:`uuid.UUID` instance. |
| 338 | :param uuid_representation: A member of |
| 339 | :class:`~bson.binary.UuidRepresentation`. Default: |
| 340 | :const:`~bson.binary.UuidRepresentation.STANDARD`. |
| 341 | See `UUID representations <https://www.mongodb.com/docs/languages/python/pymongo-driver/current/data-formats/uuid/#universally-unique-ids--uuids->`_ for details. |
| 342 | |
| 343 | .. versionadded:: 3.11 |
| 344 | """ |
| 345 | if not isinstance(uuid, UUID): |
| 346 | raise TypeError(f"uuid must be an instance of uuid.UUID, not {type(uuid)}") |
| 347 | |
| 348 | if uuid_representation not in ALL_UUID_REPRESENTATIONS: |
| 349 | raise ValueError( |
| 350 | "uuid_representation must be a value from bson.binary.UuidRepresentation" |
| 351 | ) |
| 352 | |
| 353 | if uuid_representation == UuidRepresentation.UNSPECIFIED: |
| 354 | raise ValueError( |
| 355 | "cannot encode native uuid.UUID with " |
| 356 | "UuidRepresentation.UNSPECIFIED. UUIDs can be manually " |
| 357 | "converted to bson.Binary instances using " |
| 358 | "bson.Binary.from_uuid() or a different UuidRepresentation " |
| 359 | "can be configured. See the documentation for " |
| 360 | "UuidRepresentation for more information." |
| 361 | ) |
| 362 | |
| 363 | subtype = OLD_UUID_SUBTYPE |
| 364 | if uuid_representation == UuidRepresentation.PYTHON_LEGACY: |
| 365 | payload = uuid.bytes |
| 366 | elif uuid_representation == UuidRepresentation.JAVA_LEGACY: |
| 367 | from_uuid = uuid.bytes |
| 368 | payload = from_uuid[0:8][::-1] + from_uuid[8:16][::-1] |
| 369 | elif uuid_representation == UuidRepresentation.CSHARP_LEGACY: |
| 370 | payload = uuid.bytes_le |
| 371 | else: |
| 372 | # uuid_representation == UuidRepresentation.STANDARD |
| 373 | subtype = UUID_SUBTYPE |
| 374 | payload = uuid.bytes |
| 375 | |
| 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. |
no outgoing calls