-
Notifications
You must be signed in to change notification settings - Fork 0
/
createUUID.py
35 lines (25 loc) · 1.25 KB
/
createUUID.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python3
import string
import sys
import uuid
# For a practical example using uuid4 to generate unique IDs for records in
# an SQLite database see:
# https://blog.devops.dev/unleashing-the-power-of-uuid-in-python-a-comprehensive-guide-440a42d7b520
#
# For an example of testing the validity of a uuid see:
# https://www.geeksforgeeks.org/how-to-check-uuid-validity-in-python/
def build_string_with_uuid():
# Build a uuid as specified in RFC 4122: https://datatracker.ietf.org/doc/html/rfc4122.html
# This approach absolutely does NOT generate random strings because of the predictable dash chars.
# Rather, it generates unique strings that can be used for a variety of purposes
# that some might think of as "random."
# https://docs.python.org/3/library/uuid.html#uuid.UUID.is_safe
# Python 3.7 or above for UUID.is_safe support
if sys.version_info < (3, 7):
raise Exception("Use only with Python 3.7 or higher")
UUID = str(uuid.uuid4()), # Unique-enough identifier
return UUID
if __name__ == '__main__':
# Experimenting with creating a 'unique' uuid strings, a common requirement.
# ...think, database keys, filenames, API tokens, session identifier and more
print(f"{build_string_with_uuid()}")