Skip to content

Commit 9d137ad

Browse files
jaycee-licopybara-github
authored andcommitted
chore: add a utility method for generating unique names
PiperOrigin-RevId: 495116428
1 parent d72bc83 commit 9d137ad

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

google/cloud/aiplatform/utils/__init__.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright 2020 Google LLC
3+
# Copyright 2022 Google LLC
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import logging
2323
import re
2424
from typing import Any, Callable, Dict, Optional, Type, TypeVar, Tuple
25+
import uuid
2526

2627
from google.protobuf import timestamp_pb2
2728

@@ -718,3 +719,14 @@ def get_timestamp_proto(
718719
timestamp_proto.FromDatetime(time)
719720

720721
return timestamp_proto
722+
723+
724+
def timestamped_unique_name() -> str:
725+
"""Composes a timestamped unique name.
726+
727+
Returns:
728+
A string representing a unique name.
729+
"""
730+
timestamp = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
731+
unique_id = uuid.uuid4().hex[0:5]
732+
return f"{timestamp}-{unique_id}"

tests/unit/aiplatform/test_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import importlib
2121
import json
2222
import os
23+
import re
2324
import textwrap
2425
from typing import Callable, Dict, Optional, Tuple
2526
from unittest import mock
@@ -452,6 +453,11 @@ def test_get_timestamp_proto(
452453
assert true_timestamp_proto == utils.get_timestamp_proto(time)
453454

454455

456+
def test_timestamped_unique_name():
457+
name = utils.timestamped_unique_name()
458+
assert re.match(r"\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2}-.{5}", name)
459+
460+
455461
class TestPipelineUtils:
456462
SAMPLE_JOB_SPEC = {
457463
"pipelineSpec": {

0 commit comments

Comments
 (0)