Created
December 17, 2022 07:32
-
-
Save xmonader/7ad7434ffab80fc62a95a69811da830e to your computer and use it in GitHub Desktop.
uuid.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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