blob: ea9ea7dc7d9b823a877aca961fae554c0fdcd8b9 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001: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 Reedyfa089b92016-06-11 15:02:54 -04009**Source code:** :source:`Lib/uuid.py`
10
11--------------
Georg Brandl116aa622007-08-15 14:28:22 +000012
Georg Brandl116aa622007-08-15 14:28:22 +000013This module provides immutable :class:`UUID` objects (the :class:`UUID` class)
14and the functions :func:`uuid1`, :func:`uuid3`, :func:`uuid4`, :func:`uuid5` for
15generating version 1, 3, 4, and 5 UUIDs as specified in :rfc:`4122`.
16
17If 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
19a UUID containing the computer's network address. :func:`uuid4` creates a
20random UUID.
21
Barry Warsaw8c130d72017-02-18 15:45:49 -050022Depending on support from the underlying platform, :func:`uuid1` may or may
23not return a "safe" UUID. A safe UUID is one which is generated using
24synchronization methods that ensure no two processes can obtain the same
25UUID. All instances of :class:`UUID` have an :attr:`is_safe` attribute
26which relays any information about the UUID's safety, using this enumeration:
Georg Brandl116aa622007-08-15 14:28:22 +000027
Barry Warsaw8c130d72017-02-18 15:45:49 -050028.. 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 Brandl116aa622007-08-15 14:28:22 +000046
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 Brandl1d523e12009-12-19 18:23:28 +000059 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 Brandl116aa622007-08-15 14:28:22 +000062 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 Peksag6b5e4a82016-12-31 20:08:16 +030070 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 Brandl116aa622007-08-15 14:28:22 +000077
Georg Brandl7f01a132009-09-16 15:58:14 +000078:class:`UUID` instances have these read-only attributes:
Georg Brandl116aa622007-08-15 14:28:22 +000079
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 Zhang8a179952017-01-10 11:29:27 +0800136 one of the constants :const:`RESERVED_NCS`, :const:`RFC_4122`,
Georg Brandl116aa622007-08-15 14:28:22 +0000137 :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 Warsaw8c130d72017-02-18 15:45:49 -0500145.. 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 Brandl116aa622007-08-15 14:28:22 +0000152The :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
159 attempts to obtain the hardware address fail, we choose a random 48-bit number
160 with its eighth bit set to 1 as recommended in RFC 4122. "Hardware address"
161 means the MAC address of a network interface, and on a machine with multiple
162 network interfaces the MAC address of any one of them may be returned.
163
164.. index:: single: getnode
165
166
Georg Brandl7f01a132009-09-16 15:58:14 +0000167.. function:: uuid1(node=None, clock_seq=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000168
169 Generate a UUID from a host ID, sequence number, and the current time. If *node*
170 is not given, :func:`getnode` is used to obtain the hardware address. If
171 *clock_seq* is given, it is used as the sequence number; otherwise a random
172 14-bit sequence number is chosen.
173
174.. index:: single: uuid1
175
176
177.. function:: uuid3(namespace, name)
178
179 Generate a UUID based on the MD5 hash of a namespace identifier (which is a
180 UUID) and a name (which is a string).
181
182.. index:: single: uuid3
183
184
185.. function:: uuid4()
186
187 Generate a random UUID.
188
189.. index:: single: uuid4
190
191
192.. function:: uuid5(namespace, name)
193
194 Generate a UUID based on the SHA-1 hash of a namespace identifier (which is a
195 UUID) and a name (which is a string).
196
197.. index:: single: uuid5
198
199The :mod:`uuid` module defines the following namespace identifiers for use with
200:func:`uuid3` or :func:`uuid5`.
201
202
203.. data:: NAMESPACE_DNS
204
205 When this namespace is specified, the *name* string is a fully-qualified domain
206 name.
207
208
209.. data:: NAMESPACE_URL
210
211 When this namespace is specified, the *name* string is a URL.
212
213
214.. data:: NAMESPACE_OID
215
216 When this namespace is specified, the *name* string is an ISO OID.
217
218
219.. data:: NAMESPACE_X500
220
221 When this namespace is specified, the *name* string is an X.500 DN in DER or a
222 text output format.
223
224The :mod:`uuid` module defines the following constants for the possible values
225of the :attr:`variant` attribute:
226
227
228.. data:: RESERVED_NCS
229
230 Reserved for NCS compatibility.
231
232
233.. data:: RFC_4122
234
235 Specifies the UUID layout given in :rfc:`4122`.
236
237
238.. data:: RESERVED_MICROSOFT
239
240 Reserved for Microsoft compatibility.
241
242
243.. data:: RESERVED_FUTURE
244
245 Reserved for future definition.
246
247
248.. seealso::
249
250 :rfc:`4122` - A Universally Unique IDentifier (UUID) URN Namespace
251 This specification defines a Uniform Resource Name namespace for UUIDs, the
252 internal format of UUIDs, and methods of generating UUIDs.
253
254
255.. _uuid-example:
256
257Example
258-------
259
260Here are some examples of typical usage of the :mod:`uuid` module::
261
262 >>> import uuid
263
Ezio Melottib1b3fd22011-12-02 19:28:36 +0200264 >>> # make a UUID based on the host ID and current time
Georg Brandl116aa622007-08-15 14:28:22 +0000265 >>> uuid.uuid1()
266 UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')
267
Ezio Melottib1b3fd22011-12-02 19:28:36 +0200268 >>> # make a UUID using an MD5 hash of a namespace UUID and a name
Georg Brandl116aa622007-08-15 14:28:22 +0000269 >>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
270 UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')
271
Ezio Melottib1b3fd22011-12-02 19:28:36 +0200272 >>> # make a random UUID
Georg Brandl116aa622007-08-15 14:28:22 +0000273 >>> uuid.uuid4()
274 UUID('16fd2706-8baf-433b-82eb-8c7fada847da')
275
Ezio Melottib1b3fd22011-12-02 19:28:36 +0200276 >>> # make a UUID using a SHA-1 hash of a namespace UUID and a name
Georg Brandl116aa622007-08-15 14:28:22 +0000277 >>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
278 UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')
279
Ezio Melottib1b3fd22011-12-02 19:28:36 +0200280 >>> # make a UUID from a string of hex digits (braces and hyphens ignored)
Georg Brandl116aa622007-08-15 14:28:22 +0000281 >>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')
282
Ezio Melottib1b3fd22011-12-02 19:28:36 +0200283 >>> # convert a UUID to a string of hex digits in standard form
Georg Brandl116aa622007-08-15 14:28:22 +0000284 >>> str(x)
285 '00010203-0405-0607-0809-0a0b0c0d0e0f'
286
Ezio Melottib1b3fd22011-12-02 19:28:36 +0200287 >>> # get the raw 16 bytes of the UUID
Georg Brandl116aa622007-08-15 14:28:22 +0000288 >>> x.bytes
Georg Brandl1d523e12009-12-19 18:23:28 +0000289 b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'
Georg Brandl116aa622007-08-15 14:28:22 +0000290
Ezio Melottib1b3fd22011-12-02 19:28:36 +0200291 >>> # make a UUID from a 16-byte string
Georg Brandl116aa622007-08-15 14:28:22 +0000292 >>> uuid.UUID(bytes=x.bytes)
293 UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')
294