Skip to content

Instantly share code, notes, and snippets.

@docapotamus
Last active April 7, 2017 20:00
Show Gist options
  • Save docapotamus/5a9d16a7a55d0ba6685f4e263aedb127 to your computer and use it in GitHub Desktop.
Save docapotamus/5a9d16a7a55d0ba6685f4e263aedb127 to your computer and use it in GitHub Desktop.
Handling UUID v1mc in Python
# UUID v1mc Python
import random
from uuid import uuid1
random_node = random.randrange(0, 1<<48L) | 0x010000000000L
print uuid1(random_node).hex
# Covert hex encoded UUID back to a UUID
from uuid import UUID
hex_uuid = '76d248b5392d11e6a77acb544f53c268'
print UUID(hex=hex_uuid)
# Early users where given auto incrementing IDs
# This was only roughly the first 100 users
# To turn the number into a UUID
from uuid import UUID
print UUID(int=1)
# To turn either into a UUID we need to
from uuid import UUID
try:
user_id = UUID(hex=id)
except ValueError:
user_id = UUID(int=int(id))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment