Skip to content

Instantly share code, notes, and snippets.

@xmonader
Created December 17, 2022 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xmonader/7ad7434ffab80fc62a95a69811da830e to your computer and use it in GitHub Desktop.
Save xmonader/7ad7434ffab80fc62a95a69811da830e to your computer and use it in GitHub Desktop.
uuid.py
import os
import struct
def generate_uuid():
"""Generate a random UUID."""
with open("/dev/urandom", "rb") as f:
data = f.read(16)
return struct.unpack("<LHHBBLBBBBB", data)
def uuid_to_string(uuid):
"""Convert a UUID to a string."""
return "-".join("%0.8x" % x for x in uuid)
def string_to_uuid(uuid_str):
"""Convert a string to a UUID."""
return tuple(int(x, 16) for x in uuid_str.split("-"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment