Skip to content

Commit

Permalink
one more round of version tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
pudo committed Jun 7, 2024
1 parent 51ced39 commit f64cfdb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 7 additions & 3 deletions nomenklatura/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
from typing import Any, List, Iterator, Optional
from datetime import datetime, timezone

ALPHABET = string.digits + string.ascii_lowercase
ALPHABET = string.ascii_lowercase


class Version(object):
"""A class to represent a dataset version, which consists of a timestamp
and a string tag."""

__slots__ = ["dt", "tag"]

def __init__(self, dt: datetime, tag: str) -> None:
self.dt: datetime = dt
self.tag: str = tag
Expand All @@ -23,9 +25,11 @@ def new(cls, tag: Optional[str] = None) -> "Version":

if tag is None:
# This keeps the tag sortable but short.
tag_num = ((now.microsecond // 1000) * 10) + random.randint(0, 9)
tag = cls._tag_encode(int(tag_num))
tag_num = (now.microsecond // 1000) * 10
tag_num_ = tag_num + random.randint(0, 9)
tag = cls._tag_encode(int(tag_num_))

tag = tag.ljust(3, "x")[:3]
now = now.replace(microsecond=0)
return cls(now, tag)

Expand Down
10 changes: 9 additions & 1 deletion tests/test_versions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
import pytest
from typing import Optional
from nomenklatura.versions import Version, VersionHistory


def test_version():
def test_version() -> None:
runid = Version.new("aaa")
assert runid.id.startswith(runid.dt.strftime("%Y%m%d%H%M%S"))
assert len(runid.tag) == 3
Expand All @@ -24,6 +25,13 @@ def test_version():
runid4 = Version.from_env("NK_RUN2222_ID")
assert runid4.id != runid2.id

prev_id: Optional[Version] = None
for i in range(100):
next_id = Version.new()
if prev_id is not None:
assert next_id.dt >= prev_id.dt, (next_id, prev_id)
prev_id = next_id


def test_run_history():
original = VersionHistory([])
Expand Down

0 comments on commit f64cfdb

Please sign in to comment.