_uuid.py 1.0 KB

12345678910111213141516171819202122232425262728
  1. def uuid_str_to_int(uuid_str: str) -> int:
  2. """
  3. >>> uuid_str_to_int('613ea4ac-a4cf-4026-8e99-1904b2bb5cd0')
  4. 129260377989791042510121038559452028112
  5. >>> uuid_str_to_int('779d1046-d163-4b2b-8126-254f631cb6a5')
  6. 158993652234522396927268087548725737125
  7. """
  8. return int(uuid_str.replace('-', ''), 16)
  9. def uuid_int_to_bytes(uuid_siv: int) -> bytes:
  10. """
  11. >>> uuid_int_to_bytes(129260377989791042510121038559452028112)
  12. b'a>\\xa4\\xac\\xa4\\xcf@&\\x8e\\x99\\x19\\x04\\xb2\\xbb\\\\\\xd0'
  13. >>> uuid_int_to_bytes(158993652234522396927268087548725737125)
  14. b'w\\x9d\\x10F\\xd1cK+\\x81&%Oc\\x1c\\xb6\\xa5'
  15. """
  16. return uuid_siv.to_bytes(16, byteorder='big')
  17. def uuid_str_to_bytes(uuid_str: str) -> bytes:
  18. """
  19. >>> uuid_str_to_bytes('613ea4ac-a4cf-4026-8e99-1904b2bb5cd0')
  20. b'a>\\xa4\\xac\\xa4\\xcf@&\\x8e\\x99\\x19\\x04\\xb2\\xbb\\\\\\xd0'
  21. >>> uuid_str_to_bytes('779d1046-d163-4b2b-8126-254f631cb6a5')
  22. b'w\\x9d\\x10F\\xd1cK+\\x81&%Oc\\x1c\\xb6\\xa5'
  23. """
  24. return uuid_int_to_bytes(uuid_str_to_int(uuid_str))