| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`uuid` --- UUID objects according to RFC 4122 | 
|  | 2 | ================================================== | 
|  | 3 |  | 
|  | 4 | .. module:: uuid | 
|  | 5 | :synopsis: UUID objects (universally unique identifiers) according to RFC 4122 | 
|  | 6 | .. moduleauthor:: Ka-Ping Yee <ping@zesty.ca> | 
|  | 7 | .. sectionauthor:: George Yoshida <quiver@users.sourceforge.net> | 
|  | 8 |  | 
| Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 9 | **Source code:** :source:`Lib/uuid.py` | 
|  | 10 |  | 
|  | 11 | -------------- | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | This module provides immutable :class:`UUID` objects (the :class:`UUID` class) | 
|  | 14 | and the functions :func:`uuid1`, :func:`uuid3`, :func:`uuid4`, :func:`uuid5` for | 
|  | 15 | generating version 1, 3, 4, and 5 UUIDs as specified in :rfc:`4122`. | 
|  | 16 |  | 
|  | 17 | If all you want is a unique ID, you should probably call :func:`uuid1` or | 
|  | 18 | :func:`uuid4`.  Note that :func:`uuid1` may compromise privacy since it creates | 
|  | 19 | a UUID containing the computer's network address.  :func:`uuid4` creates a | 
|  | 20 | random UUID. | 
|  | 21 |  | 
| Barry Warsaw | 8c130d7 | 2017-02-18 15:45:49 -0500 | [diff] [blame] | 22 | Depending on support from the underlying platform, :func:`uuid1` may or may | 
|  | 23 | not return a "safe" UUID.  A safe UUID is one which is generated using | 
|  | 24 | synchronization methods that ensure no two processes can obtain the same | 
|  | 25 | UUID.  All instances of :class:`UUID` have an :attr:`is_safe` attribute | 
|  | 26 | which relays any information about the UUID's safety, using this enumeration: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 27 |  | 
| Barry Warsaw | 8c130d7 | 2017-02-18 15:45:49 -0500 | [diff] [blame] | 28 | .. class:: SafeUUID | 
|  | 29 |  | 
|  | 30 | .. versionadded:: 3.7 | 
|  | 31 |  | 
|  | 32 | .. attribute:: SafeUUID.safe | 
|  | 33 |  | 
|  | 34 | The UUID was generated by the platform in a multiprocessing-safe way. | 
|  | 35 |  | 
|  | 36 | .. attribute:: SafeUUID.unsafe | 
|  | 37 |  | 
|  | 38 | The UUID was not generated in a multiprocessing-safe way. | 
|  | 39 |  | 
|  | 40 | .. attribute:: SafeUUID.unknown | 
|  | 41 |  | 
|  | 42 | The platform does not provide information on whether the UUID was | 
|  | 43 | generated safely or not. | 
|  | 44 |  | 
|  | 45 | .. class:: UUID(hex=None, bytes=None, bytes_le=None, fields=None, int=None, version=None, *, is_safe=SafeUUID.unknown) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 46 |  | 
|  | 47 | Create a UUID from either a string of 32 hexadecimal digits, a string of 16 | 
|  | 48 | bytes as the *bytes* argument, a string of 16 bytes in little-endian order as | 
|  | 49 | the *bytes_le* argument, a tuple of six integers (32-bit *time_low*, 16-bit | 
|  | 50 | *time_mid*, 16-bit *time_hi_version*, 8-bit *clock_seq_hi_variant*, 8-bit | 
|  | 51 | *clock_seq_low*, 48-bit *node*) as the *fields* argument, or a single 128-bit | 
|  | 52 | integer as the *int* argument.  When a string of hex digits is given, curly | 
|  | 53 | braces, hyphens, and a URN prefix are all optional.  For example, these | 
|  | 54 | expressions all yield the same UUID:: | 
|  | 55 |  | 
|  | 56 | UUID('{12345678-1234-5678-1234-567812345678}') | 
|  | 57 | UUID('12345678123456781234567812345678') | 
|  | 58 | UUID('urn:uuid:12345678-1234-5678-1234-567812345678') | 
| Georg Brandl | 1d523e1 | 2009-12-19 18:23:28 +0000 | [diff] [blame] | 59 | UUID(bytes=b'\x12\x34\x56\x78'*4) | 
|  | 60 | UUID(bytes_le=b'\x78\x56\x34\x12\x34\x12\x78\x56' + | 
|  | 61 | b'\x12\x34\x56\x78\x12\x34\x56\x78') | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 62 | UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678)) | 
|  | 63 | UUID(int=0x12345678123456781234567812345678) | 
|  | 64 |  | 
|  | 65 | Exactly one of *hex*, *bytes*, *bytes_le*, *fields*, or *int* must be given. | 
|  | 66 | The *version* argument is optional; if given, the resulting UUID will have its | 
|  | 67 | variant and version number set according to RFC 4122, overriding bits in the | 
|  | 68 | given *hex*, *bytes*, *bytes_le*, *fields*, or *int*. | 
|  | 69 |  | 
| Berker Peksag | 6b5e4a8 | 2016-12-31 20:08:16 +0300 | [diff] [blame] | 70 | Comparison of UUID objects are made by way of comparing their | 
|  | 71 | :attr:`UUID.int` attributes.  Comparison with a non-UUID object | 
|  | 72 | raises a :exc:`TypeError`. | 
|  | 73 |  | 
|  | 74 | ``str(uuid)`` returns a string in the form | 
|  | 75 | ``12345678-1234-5678-1234-567812345678`` where the 32 hexadecimal digits | 
|  | 76 | represent the UUID. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 77 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 78 | :class:`UUID` instances have these read-only attributes: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 79 |  | 
|  | 80 | .. attribute:: UUID.bytes | 
|  | 81 |  | 
|  | 82 | The UUID as a 16-byte string (containing the six integer fields in big-endian | 
|  | 83 | byte order). | 
|  | 84 |  | 
|  | 85 |  | 
|  | 86 | .. attribute:: UUID.bytes_le | 
|  | 87 |  | 
|  | 88 | The UUID as a 16-byte string (with *time_low*, *time_mid*, and *time_hi_version* | 
|  | 89 | in little-endian byte order). | 
|  | 90 |  | 
|  | 91 |  | 
|  | 92 | .. attribute:: UUID.fields | 
|  | 93 |  | 
|  | 94 | A tuple of the six integer fields of the UUID, which are also available as six | 
|  | 95 | individual attributes and two derived attributes: | 
|  | 96 |  | 
|  | 97 | +------------------------------+-------------------------------+ | 
|  | 98 | | Field                        | Meaning                       | | 
|  | 99 | +==============================+===============================+ | 
|  | 100 | | :attr:`time_low`             | the first 32 bits of the UUID | | 
|  | 101 | +------------------------------+-------------------------------+ | 
|  | 102 | | :attr:`time_mid`             | the next 16 bits of the UUID  | | 
|  | 103 | +------------------------------+-------------------------------+ | 
|  | 104 | | :attr:`time_hi_version`      | the next 16 bits of the UUID  | | 
|  | 105 | +------------------------------+-------------------------------+ | 
|  | 106 | | :attr:`clock_seq_hi_variant` | the next 8 bits of the UUID   | | 
|  | 107 | +------------------------------+-------------------------------+ | 
|  | 108 | | :attr:`clock_seq_low`        | the next 8 bits of the UUID   | | 
|  | 109 | +------------------------------+-------------------------------+ | 
|  | 110 | | :attr:`node`                 | the last 48 bits of the UUID  | | 
|  | 111 | +------------------------------+-------------------------------+ | 
|  | 112 | | :attr:`time`                 | the 60-bit timestamp          | | 
|  | 113 | +------------------------------+-------------------------------+ | 
|  | 114 | | :attr:`clock_seq`            | the 14-bit sequence number    | | 
|  | 115 | +------------------------------+-------------------------------+ | 
|  | 116 |  | 
|  | 117 |  | 
|  | 118 | .. attribute:: UUID.hex | 
|  | 119 |  | 
|  | 120 | The UUID as a 32-character hexadecimal string. | 
|  | 121 |  | 
|  | 122 |  | 
|  | 123 | .. attribute:: UUID.int | 
|  | 124 |  | 
|  | 125 | The UUID as a 128-bit integer. | 
|  | 126 |  | 
|  | 127 |  | 
|  | 128 | .. attribute:: UUID.urn | 
|  | 129 |  | 
|  | 130 | The UUID as a URN as specified in RFC 4122. | 
|  | 131 |  | 
|  | 132 |  | 
|  | 133 | .. attribute:: UUID.variant | 
|  | 134 |  | 
|  | 135 | The UUID variant, which determines the internal layout of the UUID. This will be | 
| Xiang Zhang | 8a17995 | 2017-01-10 11:29:27 +0800 | [diff] [blame] | 136 | one of the constants :const:`RESERVED_NCS`, :const:`RFC_4122`, | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 137 | :const:`RESERVED_MICROSOFT`, or :const:`RESERVED_FUTURE`. | 
|  | 138 |  | 
|  | 139 |  | 
|  | 140 | .. attribute:: UUID.version | 
|  | 141 |  | 
|  | 142 | The UUID version number (1 through 5, meaningful only when the variant is | 
|  | 143 | :const:`RFC_4122`). | 
|  | 144 |  | 
| Barry Warsaw | 8c130d7 | 2017-02-18 15:45:49 -0500 | [diff] [blame] | 145 | .. attribute:: UUID.is_safe | 
|  | 146 |  | 
|  | 147 | An enumeration of :class:`SafeUUID` which indicates whether the platform | 
|  | 148 | generated the UUID in a multiprocessing-safe way. | 
|  | 149 |  | 
|  | 150 | .. versionadded:: 3.7 | 
|  | 151 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 152 | The :mod:`uuid` module defines the following functions: | 
|  | 153 |  | 
|  | 154 |  | 
|  | 155 | .. function:: getnode() | 
|  | 156 |  | 
|  | 157 | Get the hardware address as a 48-bit positive integer.  The first time this | 
|  | 158 | runs, it may launch a separate program, which could be quite slow.  If all | 
| Barry Warsaw | 23df2d1 | 2017-11-28 17:26:04 -0500 | [diff] [blame^] | 159 | attempts to obtain the hardware address fail, we choose a random 48-bit | 
|  | 160 | number with the multicast bit (least significant bit of the first octet) | 
|  | 161 | set to 1 as recommended in RFC 4122.  "Hardware address" means the MAC | 
|  | 162 | address of a network interface.  On a machine with multiple network | 
|  | 163 | interfaces, universally administered MAC addresses (i.e. where the second | 
|  | 164 | least significant bit of the first octet is *unset*) will be preferred over | 
|  | 165 | locally administered MAC addresses, but with no other ordering guarantees. | 
|  | 166 |  | 
|  | 167 | .. versionchanged:: 3.7 | 
|  | 168 | Universally administered MAC addresses are preferred over locally | 
|  | 169 | administered MAC addresses, since the former are guaranteed to be | 
|  | 170 | globally unique, while the latter are not. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 171 |  | 
|  | 172 | .. index:: single: getnode | 
|  | 173 |  | 
|  | 174 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 175 | .. function:: uuid1(node=None, clock_seq=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 176 |  | 
|  | 177 | Generate a UUID from a host ID, sequence number, and the current time. If *node* | 
|  | 178 | is not given, :func:`getnode` is used to obtain the hardware address. If | 
|  | 179 | *clock_seq* is given, it is used as the sequence number; otherwise a random | 
|  | 180 | 14-bit sequence number is chosen. | 
|  | 181 |  | 
|  | 182 | .. index:: single: uuid1 | 
|  | 183 |  | 
|  | 184 |  | 
|  | 185 | .. function:: uuid3(namespace, name) | 
|  | 186 |  | 
|  | 187 | Generate a UUID based on the MD5 hash of a namespace identifier (which is a | 
|  | 188 | UUID) and a name (which is a string). | 
|  | 189 |  | 
|  | 190 | .. index:: single: uuid3 | 
|  | 191 |  | 
|  | 192 |  | 
|  | 193 | .. function:: uuid4() | 
|  | 194 |  | 
|  | 195 | Generate a random UUID. | 
|  | 196 |  | 
|  | 197 | .. index:: single: uuid4 | 
|  | 198 |  | 
|  | 199 |  | 
|  | 200 | .. function:: uuid5(namespace, name) | 
|  | 201 |  | 
|  | 202 | Generate a UUID based on the SHA-1 hash of a namespace identifier (which is a | 
|  | 203 | UUID) and a name (which is a string). | 
|  | 204 |  | 
|  | 205 | .. index:: single: uuid5 | 
|  | 206 |  | 
|  | 207 | The :mod:`uuid` module defines the following namespace identifiers for use with | 
|  | 208 | :func:`uuid3` or :func:`uuid5`. | 
|  | 209 |  | 
|  | 210 |  | 
|  | 211 | .. data:: NAMESPACE_DNS | 
|  | 212 |  | 
|  | 213 | When this namespace is specified, the *name* string is a fully-qualified domain | 
|  | 214 | name. | 
|  | 215 |  | 
|  | 216 |  | 
|  | 217 | .. data:: NAMESPACE_URL | 
|  | 218 |  | 
|  | 219 | When this namespace is specified, the *name* string is a URL. | 
|  | 220 |  | 
|  | 221 |  | 
|  | 222 | .. data:: NAMESPACE_OID | 
|  | 223 |  | 
|  | 224 | When this namespace is specified, the *name* string is an ISO OID. | 
|  | 225 |  | 
|  | 226 |  | 
|  | 227 | .. data:: NAMESPACE_X500 | 
|  | 228 |  | 
|  | 229 | When this namespace is specified, the *name* string is an X.500 DN in DER or a | 
|  | 230 | text output format. | 
|  | 231 |  | 
|  | 232 | The :mod:`uuid` module defines the following constants for the possible values | 
|  | 233 | of the :attr:`variant` attribute: | 
|  | 234 |  | 
|  | 235 |  | 
|  | 236 | .. data:: RESERVED_NCS | 
|  | 237 |  | 
|  | 238 | Reserved for NCS compatibility. | 
|  | 239 |  | 
|  | 240 |  | 
|  | 241 | .. data:: RFC_4122 | 
|  | 242 |  | 
|  | 243 | Specifies the UUID layout given in :rfc:`4122`. | 
|  | 244 |  | 
|  | 245 |  | 
|  | 246 | .. data:: RESERVED_MICROSOFT | 
|  | 247 |  | 
|  | 248 | Reserved for Microsoft compatibility. | 
|  | 249 |  | 
|  | 250 |  | 
|  | 251 | .. data:: RESERVED_FUTURE | 
|  | 252 |  | 
|  | 253 | Reserved for future definition. | 
|  | 254 |  | 
|  | 255 |  | 
|  | 256 | .. seealso:: | 
|  | 257 |  | 
|  | 258 | :rfc:`4122` - A Universally Unique IDentifier (UUID) URN Namespace | 
|  | 259 | This specification defines a Uniform Resource Name namespace for UUIDs, the | 
|  | 260 | internal format of UUIDs, and methods of generating UUIDs. | 
|  | 261 |  | 
|  | 262 |  | 
|  | 263 | .. _uuid-example: | 
|  | 264 |  | 
|  | 265 | Example | 
|  | 266 | ------- | 
|  | 267 |  | 
|  | 268 | Here are some examples of typical usage of the :mod:`uuid` module:: | 
|  | 269 |  | 
|  | 270 | >>> import uuid | 
|  | 271 |  | 
| Ezio Melotti | b1b3fd2 | 2011-12-02 19:28:36 +0200 | [diff] [blame] | 272 | >>> # make a UUID based on the host ID and current time | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 273 | >>> uuid.uuid1() | 
|  | 274 | UUID('a8098c1a-f86e-11da-bd1a-00112444be1e') | 
|  | 275 |  | 
| Ezio Melotti | b1b3fd2 | 2011-12-02 19:28:36 +0200 | [diff] [blame] | 276 | >>> # make a UUID using an MD5 hash of a namespace UUID and a name | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 277 | >>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org') | 
|  | 278 | UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e') | 
|  | 279 |  | 
| Ezio Melotti | b1b3fd2 | 2011-12-02 19:28:36 +0200 | [diff] [blame] | 280 | >>> # make a random UUID | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 281 | >>> uuid.uuid4() | 
|  | 282 | UUID('16fd2706-8baf-433b-82eb-8c7fada847da') | 
|  | 283 |  | 
| Ezio Melotti | b1b3fd2 | 2011-12-02 19:28:36 +0200 | [diff] [blame] | 284 | >>> # make a UUID using a SHA-1 hash of a namespace UUID and a name | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 285 | >>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org') | 
|  | 286 | UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d') | 
|  | 287 |  | 
| Ezio Melotti | b1b3fd2 | 2011-12-02 19:28:36 +0200 | [diff] [blame] | 288 | >>> # make a UUID from a string of hex digits (braces and hyphens ignored) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 289 | >>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}') | 
|  | 290 |  | 
| Ezio Melotti | b1b3fd2 | 2011-12-02 19:28:36 +0200 | [diff] [blame] | 291 | >>> # convert a UUID to a string of hex digits in standard form | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 292 | >>> str(x) | 
|  | 293 | '00010203-0405-0607-0809-0a0b0c0d0e0f' | 
|  | 294 |  | 
| Ezio Melotti | b1b3fd2 | 2011-12-02 19:28:36 +0200 | [diff] [blame] | 295 | >>> # get the raw 16 bytes of the UUID | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 296 | >>> x.bytes | 
| Georg Brandl | 1d523e1 | 2009-12-19 18:23:28 +0000 | [diff] [blame] | 297 | b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f' | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 298 |  | 
| Ezio Melotti | b1b3fd2 | 2011-12-02 19:28:36 +0200 | [diff] [blame] | 299 | >>> # make a UUID from a 16-byte string | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 300 | >>> uuid.UUID(bytes=x.bytes) | 
|  | 301 | UUID('00010203-0405-0607-0809-0a0b0c0d0e0f') | 
|  | 302 |  |