50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
from datetime import datetime, timezone
|
|
import hashlib
|
|
|
|
|
|
def make_pathname(filename: str, seed: int, modelname: str, counter: int, time_format: str):
|
|
filename = filename.replace("%date", get_timestamp("%Y-%m-%d"))
|
|
filename = filename.replace("%time", get_timestamp(time_format))
|
|
filename = filename.replace("%model", modelname)
|
|
filename = filename.replace("%seed", str(seed))
|
|
filename = filename.replace("%counter", str(counter))
|
|
return filename
|
|
|
|
|
|
def make_filename(filename: str, seed, modelname, counter, time_format):
|
|
filename = make_pathname(filename, seed, modelname, counter, time_format)
|
|
|
|
return get_timestamp(time_format) if filename == "" else filename
|
|
|
|
|
|
def parse_name(ckpt_name):
|
|
path = ckpt_name
|
|
filename = path.split("/")[-1]
|
|
filename = filename.split(".")[:-1]
|
|
filename = ".".join(filename)
|
|
return filename
|
|
|
|
|
|
def get_timestamp(time_format):
|
|
now = datetime.now(tz=timezone.utc)
|
|
try:
|
|
timestamp = now.strftime(time_format)
|
|
except:
|
|
timestamp = now.strftime("%Y-%m-%d-%H%M%SUTC")
|
|
|
|
return timestamp
|
|
|
|
|
|
def calculate_sha256(file_path):
|
|
sha256_hash = hashlib.sha256()
|
|
|
|
with open(file_path, "rb") as f:
|
|
# Read the file in chunks to avoid loading the entire file into memory
|
|
for byte_block in iter(lambda: f.read(4096), b""):
|
|
sha256_hash.update(byte_block)
|
|
|
|
return sha256_hash.hexdigest()
|
|
|
|
|
|
def handle_whitespace(string: str):
|
|
return string.strip().replace("\n", " ").replace("\r", " ").replace("\t", " ") |