forked from mlflow/mlflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
41 lines (33 loc) · 1.59 KB
/
__init__.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
30
31
32
33
34
35
36
37
38
39
40
41
from sys import version_info
from mlflow.utils.annotations import deprecated, experimental, keyword_only
PYTHON_VERSION = "{major}.{minor}.{micro}".format(major=version_info.major,
minor=version_info.minor,
micro=version_info.micro)
def get_major_minor_py_version(py_version):
return ".".join(py_version.split(".")[:2])
def get_unique_resource_id(max_length=None):
"""
Obtains a unique id that can be included in a resource name. This unique id is a valid
DNS subname.
:param max_length: The maximum length of the identifier
:return: A unique identifier that can be appended to a user-readable resource name to avoid
naming collisions.
"""
import uuid
import base64
if max_length is not None and max_length <= 0:
raise ValueError(
"The specified maximum length for the unique resource id must be positive!")
uuid_bytes = uuid.uuid4().bytes
# Use base64 encoding to shorten the UUID length. Note that the replacement of the
# unsupported '+' symbol maintains uniqueness because the UUID byte string is of a fixed,
# 16-byte length
uuid_b64 = base64.b64encode(uuid_bytes)
if version_info >= (3, 0):
# In Python3, `uuid_b64` is a `bytes` object. It needs to be
# converted to a string
uuid_b64 = uuid_b64.decode("ascii")
unique_id = uuid_b64.rstrip('=\n').replace("/", "-").replace("+", "AB").lower()
if max_length is not None:
unique_id = unique_id[:int(max_length)]
return unique_id