How to Create a Guid/Uuid in Python

How to create a GUID/UUID in Python

The uuid module provides immutable UUID objects (the UUID class) and the functions uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5 UUIDs as specified in RFC 4122.

If all you want is a unique ID, you should probably call uuid1() or uuid4(). Note that uuid1() may compromise privacy since it creates a UUID containing the computer’s network address. uuid4() creates a random UUID.

UUID versions 6 and 7 - new Universally Unique Identifier (UUID) formats for use in modern applications and databases (draft) rfc - are available from https://pypi.org/project/uuid6/

Docs:

  • Python 2
  • Python 3

Examples (for both Python 2 and 3):

>>> import uuid

>>> # make a random UUID
>>> uuid.uuid4()
UUID('bd65600d-8669-4903-8a14-af88203add38')

>>> # Convert a UUID to a string of hex digits in standard form
>>> str(uuid.uuid4())
'f50ec0b7-f960-400d-91f0-c42a6d44e3d0'

>>> # Convert a UUID to a 32-character hexadecimal string
>>> uuid.uuid4().hex
'9fe2c4e93f654fdbb24c02b15259716c'

How to generate a random UUID which is reproducible (with a seed) in Python

Almost there:

uuid.UUID(int=rd.getrandbits(128))

This was determined with the help of help:

>>> help(uuid.UUID.__init__)
Help on method __init__ in module uuid:

__init__(self, hex=None, bytes=None, bytes_le=None, fields=None, int=None, version=None) unbound uuid.UUID method
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-bit clock_seq_hi_variant, 8-bit clock_seq_low, 48-bit node) as
the 'fields' argument, or a single 128-bit integer as the 'int'
argument. When a string of hex digits is given, curly braces,
hyphens, and a URN prefix are all optional. For example, these
expressions all yield the same UUID:

UUID('{12345678-1234-5678-1234-567812345678}')
UUID('12345678123456781234567812345678')
UUID('urn:uuid:12345678-1234-5678-1234-567812345678')
UUID(bytes='\x12\x34\x56\x78'*4)
UUID(bytes_le='\x78\x56\x34\x12\x34\x12\x78\x56' +
'\x12\x34\x56\x78\x12\x34\x56\x78')
UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678))
UUID(int=0x12345678123456781234567812345678)

Exactly one of 'hex', 'bytes', 'bytes_le', 'fields', or 'int' must
be given. The 'version' argument is optional; if given, the resulting
UUID will have its variant and version set according to RFC 4122,
overriding the given 'hex', 'bytes', 'bytes_le', 'fields', or 'int'.

Fast GUIDs in Python

This is happening because process forking is causing each process to have a copy of the same memory, including a copy of the same PRNG state. In contrast, reseeding after a fork with SystemRandom solved this problem because the random numbers provided by SystemRandom are global to the operating system rather than to individual processes.

how to UUID create and call?

just like that, calling the method uuid.uuid4() will return an object that you can assign to a variable and use it in the code...

import uuid
myUuid = uuid.uuid4()

print(myUuid)

send(myUuid)
check(myUuid)
...

How to create a worldwide unique GUID/UUID system for Mongo with Python?

If you want a unique id, and don't want to use ObjectId, you probably want to use uuid4:

>>> import pymongo
>>> import uuid
>>> c = pymongo.Connection()
>>> uu = uuid.uuid4()
>>> uu
UUID('14a2aad7-fa01-40a4-8a80-04242b946ee4')
>>> c.test.uuidtest.insert({'_id': uu})
UUID('14a2aad7-fa01-40a4-8a80-04242b946ee4')
>>> c.test.uuidtest.find_one()
{u'_id': UUID('14a2aad7-fa01-40a4-8a80-04242b946ee4')}

generate int from a uuid

You can actually convert uuids to ints on python very easily:

>>> import uuid
>>> int(uuid.uuid4())
101044264907663221935019178350016176435

Yeah, it's a really big number, but hey, it will "never" be repeated and the solution is as clean as it could be.

Edit:
Keep in mind that this is for python 3. On python 2 this also works, BUT, the number you'll obtain will be long instead of int:

>>> import uuid
>>> int(uuid.uuid4())
314613414059294171759586868273801197923L
>>> type(int(uuid.uuid4()))
<type 'long'>


Related Topics



Leave a reply



Submit