blob: 6ab4889c6296259be58514281ea14b730fb2294e [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`uuid` --- UUID objects according to RFC 4122
3==================================================
4
5.. module:: uuid
6 :synopsis: UUID objects (universally unique identifiers) according to RFC 4122
7.. moduleauthor:: Ka-Ping Yee <ping@zesty.ca>
8.. sectionauthor:: George Yoshida <quiver@users.sourceforge.net>
9
10
Georg Brandl116aa622007-08-15 14:28:22 +000011This module provides immutable :class:`UUID` objects (the :class:`UUID` class)
12and the functions :func:`uuid1`, :func:`uuid3`, :func:`uuid4`, :func:`uuid5` for
13generating version 1, 3, 4, and 5 UUIDs as specified in :rfc:`4122`.
14
15If all you want is a unique ID, you should probably call :func:`uuid1` or
16:func:`uuid4`. Note that :func:`uuid1` may compromise privacy since it creates
17a UUID containing the computer's network address. :func:`uuid4` creates a
18random UUID.
19
20
21.. class:: UUID([hex[, bytes[, bytes_le[, fields[, int[, version]]]]]])
22
23 Create a UUID from either a string of 32 hexadecimal digits, a string of 16
24 bytes as the *bytes* argument, a string of 16 bytes in little-endian order as
25 the *bytes_le* argument, a tuple of six integers (32-bit *time_low*, 16-bit
26 *time_mid*, 16-bit *time_hi_version*, 8-bit *clock_seq_hi_variant*, 8-bit
27 *clock_seq_low*, 48-bit *node*) as the *fields* argument, or a single 128-bit
28 integer as the *int* argument. When a string of hex digits is given, curly
29 braces, hyphens, and a URN prefix are all optional. For example, these
30 expressions all yield the same UUID::
31
32 UUID('{12345678-1234-5678-1234-567812345678}')
33 UUID('12345678123456781234567812345678')
34 UUID('urn:uuid:12345678-1234-5678-1234-567812345678')
35 UUID(bytes='\x12\x34\x56\x78'*4)
36 UUID(bytes_le='\x78\x56\x34\x12\x34\x12\x78\x56' +
37 '\x12\x34\x56\x78\x12\x34\x56\x78')
38 UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678))
39 UUID(int=0x12345678123456781234567812345678)
40
41 Exactly one of *hex*, *bytes*, *bytes_le*, *fields*, or *int* must be given.
42 The *version* argument is optional; if given, the resulting UUID will have its
43 variant and version number set according to RFC 4122, overriding bits in the
44 given *hex*, *bytes*, *bytes_le*, *fields*, or *int*.
45
46:class:`UUID` instances have these read-only attributes:
47
48
49.. attribute:: UUID.bytes
50
51 The UUID as a 16-byte string (containing the six integer fields in big-endian
52 byte order).
53
54
55.. attribute:: UUID.bytes_le
56
57 The UUID as a 16-byte string (with *time_low*, *time_mid*, and *time_hi_version*
58 in little-endian byte order).
59
60
61.. attribute:: UUID.fields
62
63 A tuple of the six integer fields of the UUID, which are also available as six
64 individual attributes and two derived attributes:
65
66 +------------------------------+-------------------------------+
67 | Field | Meaning |
68 +==============================+===============================+
69 | :attr:`time_low` | the first 32 bits of the UUID |
70 +------------------------------+-------------------------------+
71 | :attr:`time_mid` | the next 16 bits of the UUID |
72 +------------------------------+-------------------------------+
73 | :attr:`time_hi_version` | the next 16 bits of the UUID |
74 +------------------------------+-------------------------------+
75 | :attr:`clock_seq_hi_variant` | the next 8 bits of the UUID |
76 +------------------------------+-------------------------------+
77 | :attr:`clock_seq_low` | the next 8 bits of the UUID |
78 +------------------------------+-------------------------------+
79 | :attr:`node` | the last 48 bits of the UUID |
80 +------------------------------+-------------------------------+
81 | :attr:`time` | the 60-bit timestamp |
82 +------------------------------+-------------------------------+
83 | :attr:`clock_seq` | the 14-bit sequence number |
84 +------------------------------+-------------------------------+
85
86
87.. attribute:: UUID.hex
88
89 The UUID as a 32-character hexadecimal string.
90
91
92.. attribute:: UUID.int
93
94 The UUID as a 128-bit integer.
95
96
97.. attribute:: UUID.urn
98
99 The UUID as a URN as specified in RFC 4122.
100
101
102.. attribute:: UUID.variant
103
104 The UUID variant, which determines the internal layout of the UUID. This will be
105 one of the integer constants :const:`RESERVED_NCS`, :const:`RFC_4122`,
106 :const:`RESERVED_MICROSOFT`, or :const:`RESERVED_FUTURE`.
107
108
109.. attribute:: UUID.version
110
111 The UUID version number (1 through 5, meaningful only when the variant is
112 :const:`RFC_4122`).
113
114The :mod:`uuid` module defines the following functions:
115
116
117.. function:: getnode()
118
119 Get the hardware address as a 48-bit positive integer. The first time this
120 runs, it may launch a separate program, which could be quite slow. If all
121 attempts to obtain the hardware address fail, we choose a random 48-bit number
122 with its eighth bit set to 1 as recommended in RFC 4122. "Hardware address"
123 means the MAC address of a network interface, and on a machine with multiple
124 network interfaces the MAC address of any one of them may be returned.
125
126.. index:: single: getnode
127
128
129.. function:: uuid1([node[, clock_seq]])
130
131 Generate a UUID from a host ID, sequence number, and the current time. If *node*
132 is not given, :func:`getnode` is used to obtain the hardware address. If
133 *clock_seq* is given, it is used as the sequence number; otherwise a random
134 14-bit sequence number is chosen.
135
136.. index:: single: uuid1
137
138
139.. function:: uuid3(namespace, name)
140
141 Generate a UUID based on the MD5 hash of a namespace identifier (which is a
142 UUID) and a name (which is a string).
143
144.. index:: single: uuid3
145
146
147.. function:: uuid4()
148
149 Generate a random UUID.
150
151.. index:: single: uuid4
152
153
154.. function:: uuid5(namespace, name)
155
156 Generate a UUID based on the SHA-1 hash of a namespace identifier (which is a
157 UUID) and a name (which is a string).
158
159.. index:: single: uuid5
160
161The :mod:`uuid` module defines the following namespace identifiers for use with
162:func:`uuid3` or :func:`uuid5`.
163
164
165.. data:: NAMESPACE_DNS
166
167 When this namespace is specified, the *name* string is a fully-qualified domain
168 name.
169
170
171.. data:: NAMESPACE_URL
172
173 When this namespace is specified, the *name* string is a URL.
174
175
176.. data:: NAMESPACE_OID
177
178 When this namespace is specified, the *name* string is an ISO OID.
179
180
181.. data:: NAMESPACE_X500
182
183 When this namespace is specified, the *name* string is an X.500 DN in DER or a
184 text output format.
185
186The :mod:`uuid` module defines the following constants for the possible values
187of the :attr:`variant` attribute:
188
189
190.. data:: RESERVED_NCS
191
192 Reserved for NCS compatibility.
193
194
195.. data:: RFC_4122
196
197 Specifies the UUID layout given in :rfc:`4122`.
198
199
200.. data:: RESERVED_MICROSOFT
201
202 Reserved for Microsoft compatibility.
203
204
205.. data:: RESERVED_FUTURE
206
207 Reserved for future definition.
208
209
210.. seealso::
211
212 :rfc:`4122` - A Universally Unique IDentifier (UUID) URN Namespace
213 This specification defines a Uniform Resource Name namespace for UUIDs, the
214 internal format of UUIDs, and methods of generating UUIDs.
215
216
217.. _uuid-example:
218
219Example
220-------
221
222Here are some examples of typical usage of the :mod:`uuid` module::
223
224 >>> import uuid
225
226 # make a UUID based on the host ID and current time
227 >>> uuid.uuid1()
228 UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')
229
230 # make a UUID using an MD5 hash of a namespace UUID and a name
231 >>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
232 UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')
233
234 # make a random UUID
235 >>> uuid.uuid4()
236 UUID('16fd2706-8baf-433b-82eb-8c7fada847da')
237
238 # make a UUID using a SHA-1 hash of a namespace UUID and a name
239 >>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
240 UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')
241
242 # make a UUID from a string of hex digits (braces and hyphens ignored)
243 >>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')
244
245 # convert a UUID to a string of hex digits in standard form
246 >>> str(x)
247 '00010203-0405-0607-0809-0a0b0c0d0e0f'
248
249 # get the raw 16 bytes of the UUID
250 >>> x.bytes
251 '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'
252
253 # make a UUID from a 16-byte string
254 >>> uuid.UUID(bytes=x.bytes)
255 UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')
256