Mã nguồn: lib/uuid.py Lib/uuid.py Mô -đun này cung cấp các đối tượng Nếu tất cả những gì bạn muốn là một ID duy nhất, có lẽ bạn nên gọi Tùy thuộc vào sự hỗ trợ từ nền tảng cơ bản, >>> import uuid >>> # make a UUID based on the host ID and current time >>> uuid.uuid1() UUID('a8098c1a-f86e-11da-bd1a-00112444be1e') >>> # make a UUID using an MD5 hash of a namespace UUID and a name >>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org') UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e') >>> # make a random UUID >>> uuid.uuid4() UUID('16fd2706-8baf-433b-82eb-8c7fada847da') >>> # make a UUID using a SHA-1 hash of a namespace UUID and a name >>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org') UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d') >>> # make a UUID from a string of hex digits (braces and hyphens ignored) >>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}') >>> # convert a UUID to a string of hex digits in standard form >>> str(x) '00010203-0405-0607-0809-0a0b0c0d0e0f' >>> # get the raw 16 bytes of the UUID >>> x.bytes b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f' >>> # make a UUID from a 16-byte string >>> uuid.UUID(bytes=x.bytes) UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')4 chuyển tiếp bất kỳ thông tin nào về sự an toàn của UUID, sử dụng bảng liệt kê này: classuuid.safeuuid¶uuid.SafeUUID¶ Mới trong phiên bản 3.7. an toàn¶¶UUID được tạo bởi nền tảng theo cách an toàn đa xử lý. không an toàn¶UUID không được tạo ra theo cách an toàn đa xử lý. không xác định¶¶Nền tảng không cung cấp thông tin về việc UUID có được tạo an toàn hay không. classuuid.uuid (hex = none, byte = none, byte_le = none, fields = none, int = none, phiên bản = không uuid.UUID(hex=None, bytes=None, bytes_le=None, fields=None, int=None, version=None, *, is_safe=SafeUUID.unknown)¶Tạo một UUID từ một chuỗi gồm 32 chữ số thập lục phân, một chuỗi 16 byte theo thứ tự lớn là đối số byte, một chuỗi 16 byte theo thứ tự ít bit time_low, 16 bit time_mid, 16 bit time_hi_version, clock 8 bit_seq_hi_variant, clock_seq_seq_low 8 bit, nút 48 bit) làm đối số trường hoặc một số nguyên 128 bit như đối số int. Khi một chuỗi các chữ số hex được đưa ra, niềng răng xoăn, dấu gạch nối và tiền tố URN đều là tùy chọn. Ví dụ, tất cả các biểu thức này đều mang lại cùng một UUID: UUID('{12345678-1234-5678-1234-567812345678}') UUID('12345678123456781234567812345678') UUID('urn:uuid:12345678-1234-5678-1234-567812345678') UUID(bytes=b'\x12\x34\x56\x78'*4) UUID(bytes_le=b'\x78\x56\x34\x12\x34\x12\x78\x56' + b'\x12\x34\x56\x78\x12\x34\x56\x78') UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678)) UUID(int=0x12345678123456781234567812345678) Chính xác một trong các hex, byte, byte_le, trường hoặc int phải được cung cấp. Đối số phiên bản là tùy chọn; Nếu được đưa ra, UUID kết quả sẽ có biến thể và số phiên bản của nó theo RFC 4122, ghi đè các bit trong hex, byte, byte_le, trường, hoặc int.RFC 4122, overriding bits in the given hex, bytes, bytes_le, fields, or int. So sánh các đối tượng UUID được thực hiện bằng cách so sánh các thuộc tính >>> import uuid >>> # make a UUID based on the host ID and current time >>> uuid.uuid1() UUID('a8098c1a-f86e-11da-bd1a-00112444be1e') >>> # make a UUID using an MD5 hash of a namespace UUID and a name >>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org') UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e') >>> # make a random UUID >>> uuid.uuid4() UUID('16fd2706-8baf-433b-82eb-8c7fada847da') >>> # make a UUID using a SHA-1 hash of a namespace UUID and a name >>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org') UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d') >>> # make a UUID from a string of hex digits (braces and hyphens ignored) >>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}') >>> # convert a UUID to a string of hex digits in standard form >>> str(x) '00010203-0405-0607-0809-0a0b0c0d0e0f' >>> # get the raw 16 bytes of the UUID >>> x.bytes b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f' >>> # make a UUID from a 16-byte string >>> uuid.UUID(bytes=x.bytes) UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')5 của chúng. So sánh với một đối tượng không thuộc UUID tăng >>> import uuid >>> # make a UUID based on the host ID and current time >>> uuid.uuid1() UUID('a8098c1a-f86e-11da-bd1a-00112444be1e') >>> # make a UUID using an MD5 hash of a namespace UUID and a name >>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org') UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e') >>> # make a random UUID >>> uuid.uuid4() UUID('16fd2706-8baf-433b-82eb-8c7fada847da') >>> # make a UUID using a SHA-1 hash of a namespace UUID and a name >>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org') UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d') >>> # make a UUID from a string of hex digits (braces and hyphens ignored) >>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}') >>> # convert a UUID to a string of hex digits in standard form >>> str(x) '00010203-0405-0607-0809-0a0b0c0d0e0f' >>> # get the raw 16 bytes of the UUID >>> x.bytes b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f' >>> # make a UUID from a 16-byte string >>> uuid.UUID(bytes=x.bytes) UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')6. >>> import uuid >>> # make a UUID based on the host ID and current time >>> uuid.uuid1() UUID('a8098c1a-f86e-11da-bd1a-00112444be1e') >>> # make a UUID using an MD5 hash of a namespace UUID and a name >>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org') UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e') >>> # make a random UUID >>> uuid.uuid4() UUID('16fd2706-8baf-433b-82eb-8c7fada847da') >>> # make a UUID using a SHA-1 hash of a namespace UUID and a name >>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org') UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d') >>> # make a UUID from a string of hex digits (braces and hyphens ignored) >>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}') >>> # convert a UUID to a string of hex digits in standard form >>> str(x) '00010203-0405-0607-0809-0a0b0c0d0e0f' >>> # get the raw 16 bytes of the UUID >>> x.bytes b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f' >>> # make a UUID from a 16-byte string >>> uuid.UUID(bytes=x.bytes) UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')7 Trả về một chuỗi ở dạng >>> import uuid >>> # make a UUID based on the host ID and current time >>> uuid.uuid1() UUID('a8098c1a-f86e-11da-bd1a-00112444be1e') >>> # make a UUID using an MD5 hash of a namespace UUID and a name >>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org') UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e') >>> # make a random UUID >>> uuid.uuid4() UUID('16fd2706-8baf-433b-82eb-8c7fada847da') >>> # make a UUID using a SHA-1 hash of a namespace UUID and a name >>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org') UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d') >>> # make a UUID from a string of hex digits (braces and hyphens ignored) >>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}') >>> # convert a UUID to a string of hex digits in standard form >>> str(x) '00010203-0405-0607-0809-0a0b0c0d0e0f' >>> # get the raw 16 bytes of the UUID >>> x.bytes b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f' >>> # make a UUID from a 16-byte string >>> uuid.UUID(bytes=x.bytes) UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')8 trong đó 32 chữ số thập lục phân đại diện cho UUID.
UUID dưới dạng chuỗi 16 byte (chứa sáu trường số nguyên theo thứ tự byte lớn). Uuid.bytes_le¶bytes_le¶UUID dưới dạng chuỗi 16 byte (với Time_Low, Time_Mid và Time_Hi_Version theo thứ tự byte Little-endian). UUID.Fields¶fields¶Một tuple của sáu trường số nguyên của UUID, cũng có sẵn dưới dạng sáu thuộc tính riêng lẻ và hai thuộc tính dẫn xuất:
UUID dưới dạng chuỗi thập lục phân 32 ký tự. UUID.Int¶int¶UUID dưới dạng số nguyên 128 bit. Uuid.urn¶urn¶UUID dưới dạng bình theo quy định trong RFC 4122.RFC 4122. Uuid.variant¶variant¶Biến thể UUID, xác định bố cục bên trong của UUID. Đây sẽ là một trong những hằng số Số phiên bản UUID (1 đến 5, chỉ có ý nghĩa khi biến thể là Việc liệt kê Mới trong phiên bản 3.7. an toàn¶ UUID được tạo bởi nền tảng theo cách an toàn đa xử lý.getnode()¶không an toànRFC 4122. “Hardware address” means the MAC address of a network interface. On a machine with multiple network interfaces, universally administered MAC addresses (i.e. where the second least significant bit of the first octet is unset) will be preferred over locally administered MAC addresses, but with no other ordering guarantees. UUID không được tạo ra theo cách an toàn đa xử lý.Universally administered MAC addresses are preferred over locally administered MAC addresses, since the former are guaranteed to be globally unique, while the latter are not. không xác định¶uuid1(node=None, clock_seq=None)¶Tạo UUID từ ID máy chủ, số thứ tự và thời gian hiện tại. Nếu nút không được cung cấp, Tạo UUID dựa trên băm MD5 của định danh không gian tên (là UUID) và một tên (là một chuỗi). uuid.uuid4 ()uuid4()¶Tạo UUID ngẫu nhiên. uuid.uuid5 (không gian tên, tên) ¶uuid5(namespace, name)¶Tạo UUID dựa trên băm SHA-1 của định danh không gian tên (là UUID) và một tên (là một chuỗi). Mô -đun Khi không gian tên này được chỉ định, chuỗi tên là một tên miền đủ điều kiện. uuid.namespace_url¶NAMESPACE_URL¶Khi không gian tên này được chỉ định, chuỗi tên là URL. uuid.namespace_oid¶NAMESPACE_OID¶Khi không gian tên này được chỉ định, chuỗi tên là một iso oid. uuid.namespace_x500¶NAMESPACE_X500¶Khi không gian tên này được chỉ định, chuỗi tên là X.500 DN ở định dạng đầu ra văn bản. Mô -đun Dành riêng cho khả năng tương thích NCS. uuid.rfc_4122¶RFC_4122¶Chỉ định bố cục UUID được đưa ra trong RFC 4122.RFC 4122. uuid.reserved_microsoft¶RESERVED_MICROSOFT¶Dành cho khả năng tương thích của Microsoft. uuid.reserved_future¶RESERVED_FUTURE¶Dành riêng cho định nghĩa trong tương lai. Xem thêm RFC 4122 - Một không gian tên định danh độc đáo (UUID) phổ biến - A Universally Unique IDentifier (UUID) URN NamespaceThông số kỹ thuật này xác định một tên tên tên tài nguyên thống nhất cho UUID, định dạng nội bộ của UUID và các phương thức tạo UUID. Thí dụ¶Dưới đây là một số ví dụ về cách sử dụng điển hình của mô -đun >>> import uuid >>> # make a UUID based on the host ID and current time >>> uuid.uuid1() UUID('a8098c1a-f86e-11da-bd1a-00112444be1e') >>> # make a UUID using an MD5 hash of a namespace UUID and a name >>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org') UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e') >>> # make a random UUID >>> uuid.uuid4() UUID('16fd2706-8baf-433b-82eb-8c7fada847da') >>> # make a UUID using a SHA-1 hash of a namespace UUID and a name >>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org') UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d') >>> # make a UUID from a string of hex digits (braces and hyphens ignored) >>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}') >>> # convert a UUID to a string of hex digits in standard form >>> str(x) '00010203-0405-0607-0809-0a0b0c0d0e0f' >>> # get the raw 16 bytes of the UUID >>> x.bytes b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f' >>> # make a UUID from a 16-byte string >>> uuid.UUID(bytes=x.bytes) UUID('00010203-0405-0607-0809-0a0b0c0d0e0f') |