MCPcopy Index your code
hub / github.com/python/cpython / __init__

Method __init__

Lib/uuid.py:178–265  ·  view source on GitHub ↗

r"""Create a UUID from either a string of 32 hexadecimal digits, a string of 16 bytes as the 'bytes' argument, a string of 16 bytes in little-endian order as the 'bytes_le' argument, a tuple of six integers (32-bit time_low, 16-bit time_mid, 16-bit time_hi_version, 8-

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

Source from the content-addressed store, hash-verified

176 __slots__ = ('int', 'is_safe', '__weakref__')
177
178 def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
179 int=None, version=None,
180 *, is_safe=SafeUUID.unknown):
181 r"""Create a UUID from either a string of 32 hexadecimal digits,
182 a string of 16 bytes as the 'bytes' argument, a string of 16 bytes
183 in little-endian order as the 'bytes_le' argument, a tuple of six
184 integers (32-bit time_low, 16-bit time_mid, 16-bit time_hi_version,
185 8-bit clock_seq_hi_variant, 8-bit clock_seq_low, 48-bit node) as
186 the 'fields' argument, or a single 128-bit integer as the 'int'
187 argument. When a string of hex digits is given, curly braces,
188 hyphens, and a URN prefix are all optional. For example, these
189 expressions all yield the same UUID:
190
191 UUID('{12345678-1234-5678-1234-567812345678}')
192 UUID('12345678123456781234567812345678')
193 UUID('urn:uuid:12345678-1234-5678-1234-567812345678')
194 UUID(bytes='\x12\x34\x56\x78'*4)
195 UUID(bytes_le='\x78\x56\x34\x12\x34\x12\x78\x56' +
196 '\x12\x34\x56\x78\x12\x34\x56\x78')
197 UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678))
198 UUID(int=0x12345678123456781234567812345678)
199
200 Exactly one of 'hex', 'bytes', 'bytes_le', 'fields', or 'int' must
201 be given. The 'version' argument is optional; if given, the resulting
202 UUID will have its variant and version set according to RFC 4122,
203 overriding the given 'hex', 'bytes', 'bytes_le', 'fields', or 'int'.
204
205 is_safe is an enum exposed as an attribute on the instance. It
206 indicates whether the UUID has been generated in a way that is safe
207 for multiprocessing applications, via uuid_generate_time_safe(3).
208 """
209
210 if [hex, bytes, bytes_le, fields, int].count(None) != 4:
211 raise TypeError('one of the hex, bytes, bytes_le, fields, '
212 'or int arguments must be given')
213 if int is not None:
214 pass
215 elif hex is not None:
216 hex = hex.replace('urn:', '').replace('uuid:', '')
217 hex = hex.strip('{}').replace('-', '')
218 if len(hex) != 32:
219 raise ValueError('badly formed hexadecimal UUID string')
220 int = int_(hex, 16)
221 elif bytes_le is not None:
222 if len(bytes_le) != 16:
223 raise ValueError('bytes_le is not a 16-char string')
224 assert isinstance(bytes_le, bytes_), repr(bytes_le)
225 bytes = (bytes_le[4-1::-1] + bytes_le[6-1:4-1:-1] +
226 bytes_le[8-1:6-1:-1] + bytes_le[8:])
227 int = int_.from_bytes(bytes) # big endian
228 elif bytes is not None:
229 if len(bytes) != 16:
230 raise ValueError('bytes is not a 16-char string')
231 assert isinstance(bytes, bytes_), repr(bytes)
232 int = int_.from_bytes(bytes) # big endian
233 elif fields is not None:
234 if len(fields) != 6:
235 raise ValueError('fields is not a 6-tuple')

Callers

nothing calls this directly

Calls 4

countMethod · 0.45
replaceMethod · 0.45
stripMethod · 0.45
__setattr__Method · 0.45

Tested by

no test coverage detected