Skip to content

Instantly share code, notes, and snippets.

@whwkong
Last active November 6, 2020 00:17
Show Gist options
  • Select an option

  • Save whwkong/d16a6100728d0b721329d71fc2f88b25 to your computer and use it in GitHub Desktop.

Select an option

Save whwkong/d16a6100728d0b721329d71fc2f88b25 to your computer and use it in GitHub Desktop.
Checks if string is valid uuid4
from uuid import UUID
def is_valid_uuid4(uuid_to_test):
"""
Check if uuid_to_test is a valid UUID, version 4.
Args:
uuid_to_test : str
Returns:
True if uuid_to_test is a valid UUID, otherwise `False`.
"""
try:
uuid_obj = UUID(uuid_to_test, version=4)
except (AttributeError, TypeError, ValueError):
return False
return str(uuid_obj) == uuid_to_test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment