File size: 1,198 Bytes
783a952
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
# ml_module/utils/helpers.py
from __future__ import annotations

import datetime
import hashlib
import json
import uuid
from typing import Any


def generate_project_id() -> str:
    """Generates a unique identifier for a new project."""
    return f"proj_{uuid.uuid4().hex[:12]}"


def compute_checksum(data: bytes) -> str:
    """Return a stable SHA-256 checksum for the provided bytes."""
    return hashlib.sha256(data).hexdigest()


def json_bytes(payload: Any) -> bytes:
    """Serialize payload to canonical JSON bytes for hashing/persistence."""
    if isinstance(payload, (bytes, bytearray)):
        return bytes(payload)
    if isinstance(payload, str):
        return payload.encode("utf-8")
    return json.dumps(payload, sort_keys=True, separators=(",", ":")).encode("utf-8")


def ensure_bytes(value: Any) -> bytes:
    if isinstance(value, (bytes, bytearray)):
        return bytes(value)
    if isinstance(value, str):
        return value.encode("utf-8")
    raise TypeError("Expected bytes or string value for ensure_bytes")


def utc_now() -> datetime.datetime:
    return datetime.datetime.now(datetime.timezone.utc)


def utc_now_iso() -> str:
    return utc_now().isoformat()