A Unique ID -short for Unique Identifier- is a string of characters to uniquely identify a particular entity within a system or context. It provides a unique label or identifier for that entity, distinguishing it from others. Unique keys/Ids are used to database management, networking, and identification systems that matters each data separately.
UUID
In Python, uuid
is a built-in module that provides functions for generating Universally Unique Identifiers (UUIDs).
UUID can bu used for:
- Generate Unique Random Id
- Hashing
uuid.uuid4()
This function provides and guarantees the random unique id without compromise with privacy.
1 2 3 4 5 6 7 | from uuid import uuid4 from uuid import UUID unique_key: UUID = uuid4() print("Your Unique Key: ",unique_key) >>> Your Unique Key: b2993f44-4f71-40db-93aa-7ac6cb9f8342 |
also function provides hex without hypens:
1 2 3 4 5 6 7 | from uuid import uuid4 from uuid import UUID unique_key: UUID = uuid4() print("Your Unique Key Without hypens: ",unique_key.hex) >>> Your Unique Key Without hypens: 732617e396f4434c894dbe79d6b6a1ed |
uuid.uuid1()
uuid.uuid1() function is defined in UUID library and provides generating random ids using MAC address and time component.
What is Mac Address: Media Access Control address, is a unique identifier assigned to network interfaces for communications on a network segment.
1 2 3 4 5 6 7 | from uuid import uuid1 from uuid import UUID random_uuid1: UUID = uuid1() print(random_uuid1) >>> 6f5bc70c-cfbc-11ee-9005-701ab89f0b63 |
Representations of uuid1()
- bytes : Returns id in form of 16 byte string.
1 2 3 4 5 6 7 | from uuid import uuid1 from uuid import UUID random_uuid1: UUID = uuid1() print("Byte Representation: ", random_uuid1.bytes) # >>> Byte Representation: b'\xdf*\t.\xcf\xbc\x11\xee\xb8\x9bp\x1a\xb8\x9f\x0bc' |
- int : Returns id in form of 128-bit integer.
1 2 3 4 5 6 7 8 | from uuid import uuid1 from uuid import UUID random_uuid1: UUID = uuid1() print("Integer Representation: ", random_uuid1.int) # >>> Integer Representation: 296636105778567370710104227138781514595 |
- hex : Returns random id as 32 character hexadecimal string.
1 2 3 4 5 6 7 8 | from uuid import uuid1 from uuid import UUID random_uuid1: UUID = uuid1() print("Hex Representation: ", random_uuid1.hex) # >>> Hex Representation: df2a092ecfbc11eeb89b701ab89f0b63 |
Components of uuid1()
- version : version number of UUID.
1 2 3 4 5 6 7 | from uuid import uuid1 from uuid import UUID random_uuid1: UUID = uuid1() print("Version: ", random_uuid1.version) # >>> Version: 1 |
- variant : The variant determining the internal layout of UUID.
1 2 3 4 5 6 7 8 | from uuid import uuid1 from uuid import UUID random_uuid1: UUID = uuid1() print("Variant: ", random_uuid1.variant) # >>> Variant: specified in RFC 4122 |