"uuid" --- **RFC 9562** 定義的 UUID 物件
****************************************

**原始碼：**Lib/uuid.py

======================================================================

This module provides immutable "UUID" objects (the "UUID" class) and
functions for generating UUIDs corresponding to a specific UUID
version as specified in **RFC 9562** (which supersedes **RFC 4122**),
for example, "uuid1()" for UUID version 1, "uuid3()" for UUID version
3, and so on. Note that UUID version 2 is deliberately omitted as it
is outside the scope of the RFC.

如果你只需要一個唯一的 ID，你應該呼叫 "uuid1()" 或 "uuid4()"。需要注意
的是，"uuid1()" 可能會危害隱私，因為它會建立一個包含了電腦網路位址的
UUID。而 "uuid4()" 則會建立一個隨機的 UUID。

根據底層平台的支援情況，"uuid1()" 可能會也可能不會回傳一個「安全的」
UUID。安全的 UUID 是使用同步方法生成的，以確保不會有兩個行程取得到相同
的 UUID。所有 "UUID" 的實例都有一個 "is_safe" 屬性，該屬性使用下面這個
列舉來傳遞有關 UUID 安全性的任何資訊：

class uuid.SafeUUID

   在 3.7 版被加入.

   safe

      該 UUID 是由平台以多行程安全的 (multiprocessing-safe) 方式生成的
      。

   unsafe

      該 UUID 不是以多行程安全的方式生成的。

   unknown

      該平台不提供 UUID 是否為安全生成的資訊。

class uuid.UUID(hex=None, bytes=None, bytes_le=None, fields=None, int=None, version=None, *, is_safe=SafeUUID.unknown)

   從以下其中一種引數來建立 UUID：由 32 個十六進位的數字組成的字串、以
   大端順序 (big-endian) 排列的 16 個位元組組成的字串作為 *bytes* 引數
   、以小端順序 (little-endian) 排列的 16 個位元組組成的字串作為
   *bytes_le* 引數、由 6 個整數（32 位元的 *time_low*、16 位元的
   *time_mid*、16 位元的 *time_hi_version*、8 位元的
   *clock_seq_hi_variant*、8 位元的 *clock_seq_low*、48 位元的 *node*
   ）組成的元組 (tuple) 作為 *fields* 引數，或者是單一的 128 位元整數
   作為 *int* 引數。當給定由十六進位的數字組成的字串時，大括號、連字符
   號和 URN 前綴都是可以選用的。例如，以下這些運算式都會產生相同的
   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)

   必須正好給定其中一個引數 *hex*、*bytes*、*bytes_le*、*fields* 或
   *int*。*version* 引數是選用的；如果給定了該引數，生成的 UUID 將根據
   **RFC 9562** 設置其變體 (variant) 和版本號，覆蓋掉給定的 *hex*、
   *bytes*、*bytes_le*、*fields* 或 *int* 中的位元。

   UUID 物件之間的比較是透過比較它們的 "UUID.int" 屬性。與非 UUID 的物
   件進行比較會引發 "TypeError"。

   "str(uuid)" 會回傳一個像是 "12345678-1234-5678-1234-567812345678"
   形式的字串，其中 32 個十六進位的數字代表 UUID。

"UUID" 實例有以下唯讀的屬性：

UUID.bytes

   UUID 以 16 位元組的字串表示（包含 6 個整數欄位，按照大端位元組順序
   排列）。

UUID.bytes_le

   UUID 以 16 位元組的字串表示（其中 *time_low*、*time_mid* 和
   *time_hi_version* 按照小端位元組順序排列）。

UUID.fields

   UUID 以 6 個整數欄位所組成的元組表示，也可以看作有 6 個個別屬性和 2
   個衍生屬性：

+----------------------------------------------------+----------------------------------------------------+
| 欄位                                               | 意義                                               |
+----------------------------------------------------+----------------------------------------------------+
| UUID.time_low                                      | UUID 的前 32 位元。只與版本 1 有關。               |
+----------------------------------------------------+----------------------------------------------------+
| UUID.time_mid                                      | UUID 接下來的 16 位元。只與版本 1 有關。           |
+----------------------------------------------------+----------------------------------------------------+
| UUID.time_hi_version                               | UUID 接下來的 16 位元。只與版本 1 有關。           |
+----------------------------------------------------+----------------------------------------------------+
| UUID.clock_seq_hi_variant                          | UUID 接下來的 8 位元。只與版本 1 和 6 有關。       |
+----------------------------------------------------+----------------------------------------------------+
| UUID.clock_seq_low                                 | UUID 接下來的 8 位元。只與版本 1 和 6 有關。       |
+----------------------------------------------------+----------------------------------------------------+
| UUID.node                                          | UUID 最後的 48 位元。只與版本 1 有關。             |
+----------------------------------------------------+----------------------------------------------------+
| UUID.time                                          | The 60-bit timestamp as a count of 100-nanosecond  |
|                                                    | intervals since Gregorian epoch (1582-10-15        |
|                                                    | 00:00:00) for versions 1 and 6, or the 48-bit      |
|                                                    | timestamp in milliseconds since Unix epoch         |
|                                                    | (1970-01-01 00:00:00) for version 7.               |
+----------------------------------------------------+----------------------------------------------------+
| UUID.clock_seq                                     | 14 位元的序列號。只與版本 1 和 6 有關。            |
+----------------------------------------------------+----------------------------------------------------+

UUID.hex

   UUID 以 32 個小寫十六進位字元組成的字串表示。

UUID.int

   UUID 以 128 位元的整數表示。

UUID.urn

   UUID 以 **RFC 9562** 中指定的 URN 形式表示。

UUID.variant

   UUID 的變體，決定 UUID 內部的佈局 (layout)，是 "RESERVED_NCS"、
   "RFC_4122"、"RESERVED_MICROSOFT" 或 "RESERVED_FUTURE" 其中一個常數
   。

UUID.version

   UUID 的版本號（1 到 8，只有當變體是 "RFC_4122" 時才有意義）。

   在 3.14 版的變更: 新增了 UUID 版本 6、7 和 8。

UUID.is_safe

   "SafeUUID" 的列舉，表示平台是否以多行程安全的方式產生 UUID。

   在 3.7 版被加入.

"uuid" 模組定義了以下函式：

uuid.getnode()

   取得 48 位元正整數形式的硬體位址。第一次執行時，有可能會啟動一個獨
   立的程式，這也許會相當耗時。如果所有取得硬體位址的嘗試都失敗，我們
   會根據 **RFC 4122** 中的建議，使用一個 48 位元的隨機數，其中群播位
   元 (multicast bit)（第一個八位元組的最低有效位）設置為 1。「硬體位
   址」指的是網路介面 (network interface) 的 MAC 位址。在具有多個網路
   介面的機器上，將優先選擇通用管理 (universally administered) 的 MAC
   位址（即第一個八位元組的第二個最低有效位是 *未設置的*），而不是本地
   管理 (locally administered) 的 MAC 位址，除此之外不保證任何選擇的順
   序。

   在 3.7 版的變更: 通用管理的 MAC 位址優於本地管理的 MAC 位址，因為前
   者保證是全球唯一的，而後者不是。

uuid.uuid1(node=None, clock_seq=None)

   Generate a UUID from a host ID, sequence number, and the current
   time according to **RFC 9562, §5.1**.

   When *node* is not specified, "getnode()" is used to obtain the
   hardware address as a 48-bit positive integer. When a sequence
   number *clock_seq* is not specified, a pseudo-random 14-bit
   positive integer is generated.

   If *node* or *clock_seq* exceed their expected bit count, only
   their least significant bits are kept.

uuid.uuid3(namespace, name)

   根據 **RFC 9562, §5.3**，基於命名空間識別碼 (namespace identifier)
   （一個 UUID）和名稱（一個 "bytes" 物件或使用 UTF-8 編碼的字串）的
   MD5 hash 來生成 UUID。

uuid.uuid4()

   Generate a random UUID in a cryptographically-secure method
   according to **RFC 9562, §5.4**.

uuid.uuid5(namespace, name)

   根據 **RFC 9562, §5.5**，基於命名空間識別碼（一個 UUID）和名稱（一
   個 "bytes" 物件或使用 UTF-8 編碼的字串）的 SHA-1 hash 來生成 UUID。

uuid.uuid6(node=None, clock_seq=None)

   Generate a UUID from a sequence number and the current time
   according to **RFC 9562, §5.6**.

   This is an alternative to "uuid1()" to improve database locality.

   When *node* is not specified, "getnode()" is used to obtain the
   hardware address as a 48-bit positive integer. When a sequence
   number *clock_seq* is not specified, a pseudo-random 14-bit
   positive integer is generated.

   If *node* or *clock_seq* exceed their expected bit count, only
   their least significant bits are kept.

   在 3.14 版被加入.

uuid.uuid7()

   Generate a time-based UUID according to **RFC 9562, §5.7**.

   For portability across platforms lacking sub-millisecond precision,
   UUIDs produced by this function embed a 48-bit timestamp and use a
   42-bit counter to guarantee monotonicity within a millisecond.

   在 3.14 版被加入.

uuid.uuid8(a=None, b=None, c=None)

   Generate a pseudo-random UUID according to **RFC 9562, §5.8**.

   When specified, the parameters *a*, *b* and *c* are expected to be
   positive integers of 48, 12 and 62 bits respectively. If they
   exceed their expected bit count, only their least significant bits
   are kept; non-specified arguments are substituted for a pseudo-
   random integer of appropriate size.

   By default, *a*, *b* and *c* are not generated by a
   cryptographically secure pseudo-random number generator (CSPRNG).
   Use "uuid4()" when a UUID needs to be used in a security-sensitive
   context.

   在 3.14 版被加入.

"uuid" 模組為 "uuid3()" 或 "uuid5()" 定義了以下的命名空間識別碼。

uuid.NAMESPACE_DNS

   當指定這個命名空間時，*name* 字串是一個完整網域名稱 (fully
   qualified domain name)。

uuid.NAMESPACE_URL

   當指定這個命名空間時，*name* 字串是一個 URL。

uuid.NAMESPACE_OID

   當指定這個命名空間時，*name* 字串是一個 ISO OID。

uuid.NAMESPACE_X500

   當指定這個命名空間時，*name* 字串是以 DER 或文字輸出格式表示的
   X.500 DN。

"uuid" 模組為 "variant" 屬性的可能值定義了以下常數：

uuid.RESERVED_NCS

   保留供 NCS 相容性使用。

uuid.RFC_4122

   Specifies the UUID layout given in **RFC 4122**. This constant is
   kept for backward compatibility even though **RFC 4122** has been
   superseded by **RFC 9562**.

uuid.RESERVED_MICROSOFT

   保留供 Microsoft 相容性使用。

uuid.RESERVED_FUTURE

   保留供未來定義使用。

"uuid" 模組定義了特殊的 Nil 和 Max UUID 值：

uuid.NIL

   A special form of UUID that is specified to have all 128 bits set
   to zero according to **RFC 9562, §5.9**.

   在 3.14 版被加入.

uuid.MAX

   A special form of UUID that is specified to have all 128 bits set
   to one according to **RFC 9562, §5.10**.

   在 3.14 版被加入.

也參考:

  **RFC 9562** - 通用唯一辨識碼 (UUID, Universally Unique IDentifier)
  的 URN 命名空間
     這個規範定義了 UUID 的統一資源名稱 (Uniform Resource Name) 命名空
     間、UUID 的內部格式和生成 UUID 的方法。


命令列的用法
============

在 3.12 版被加入.

"uuid" 模組可以在命令列下作為腳本來執行。

   python -m uuid [-h] [-u {uuid1,uuid3,uuid4,uuid5,uuid6,uuid7,uuid8}] [-n NAMESPACE] [-N NAME]

可以接受以下選項：

-h, --help

   顯示幫助訊息並退出。

-u <uuid>
--uuid <uuid>

   指定要用來生成 UUID 的函式名稱。預設使用 "uuid4()"。

   在 3.14 版的變更: Allow generating UUID versions 6, 7 and 8.

-n <namespace>
--namespace <namespace>

   該命名空間是一個 "UUID" 或 "@ns"，其中 "ns" 是指知名預定義 UUID 的
   命名空間名稱，例如 "@dns"、"@url"、"@oid" 和 "@x500"。 只有
   "uuid3()" / "uuid5()" 函式會需要。

-N <name>
--name <name>

   用於生成 uuid 的名稱。只有 "uuid3()" / "uuid5()" 函式會需要。

-C <num>
--count <num>

   生成 *num* 個新 UUID。

   在 3.14 版被加入.


範例
====

以下是一些 "uuid" 模組的典型使用範例：

   >>> 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')

   >>> # get the Nil UUID
   >>> uuid.NIL
   UUID('00000000-0000-0000-0000-000000000000')

   >>> # get the Max UUID
   >>> uuid.MAX
   UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')

   >>> # same as UUIDv1 but with fields reordered to improve DB locality
   >>> uuid.uuid6()
   UUID('1f0799c0-98b9-62db-92c6-a0d365b91053')

   >>> # get UUIDv7 creation (local) time as a timestamp in milliseconds
   >>> u = uuid.uuid7()
   >>> u.time
   1743936859822

   >>> # get UUIDv7 creation (local) time as a datetime object
   >>> import datetime as dt
   >>> dt.datetime.fromtimestamp(u.time / 1000)
   datetime.datetime(...)

   >>> # make a UUID with custom blocks
   >>> uuid.uuid8(0x12345678, 0x9abcdef0, 0x11223344)
   UUID('00001234-5678-8ef0-8000-000011223344')


命令列的範例
============

以下是一些 "uuid" 命令列介面的典型使用範例：

   # 生成一個隨機的 UUID - 預設使用 uuid4()
   $ python -m uuid

   # 使用 uuid1() 生成一組 UUID
   $ python -m uuid -u uuid1

   # 使用 uuid5 生成一組 UUID
   $ python -m uuid -u uuid5 -n @url -N example.com

   # 生成 42 個隨機 UUID
   $ python -m uuid -C 42
