Open nlathia opened 1 year ago
Example from slack:
import uuid
from datetime import datetime, timezone
from typing import List
from packaging.version import parse, Version
class CalVerUUID:
"""
A class to generate, parse and sort version strings based on Calendar Versioning (CalVer)
with an appended UUID for ensuring uniqueness.
"""
sorting_key = parse
@staticmethod
def generate_version() -> str:
"""
Generates a new version string with the current UTC timestamp down to the second and a unique UUID.
The returned string has the format: 'YYYY.MM.DD.HH.MM.SS+UUID'
Returns:
--------
str
A version string.
Example:
--------
>>> print(CalVerUUID.generate_version())
'2023.06.25.15.30.45+1a2b3c4d'
"""
now = datetime.now(tz=timezone.utc)
timestamp_str = now.strftime("%Y.%m.%d.%H.%M.%S")
# Generate a short unique identifier
uuid_str = str(uuid.uuid4())[:8]
return f"{timestamp_str}+{uuid_str}"
@staticmethod
def parse_version(version_str: str) -> Version:
return parse(version_str)
@staticmethod
def sort_versions(version_list: List[str]) -> List[str]:
"""
Sorts a list of version strings based on their timestamp and returns the sorted list.
"""
return sorted(version_list, key=CalVerUUID.sorting_key, reverse=True)
https://mlops-community.slack.com/archives/C0227QJCDS8/p1687777453332889?thread_ts=1687777412.934619&cid=C0227QJCDS8