__init__.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import re
  2. def str2bool(str):
  3. return True if str.lower() == 'true' else False
  4. def get_newest_ckpt(string_list):
  5. # 定义一个正则表达式模式,用于匹配字符串中的数字
  6. pattern = r'epoch=(\d+)-step=(\d+)\.ckpt'
  7. # 使用正则表达式提取每个字符串中的数字信息,并创建一个包含元组的列表
  8. extracted_info = []
  9. for string in string_list:
  10. match = re.match(pattern, string)
  11. if match:
  12. epoch = int(match.group(1))
  13. step = int(match.group(2))
  14. extracted_info.append((epoch, step, string))
  15. # 按照 epoch 后面的数字和 step 后面的数字进行排序
  16. sorted_info = sorted(
  17. extracted_info, key=lambda x: (x[0], x[1]), reverse=True)
  18. # 获取最新的 ckpt 文件名
  19. newest_ckpt = sorted_info[0][2]
  20. return newest_ckpt
  21. # 文本存在且不为空时 return True
  22. def check_txt_file(file_path):
  23. try:
  24. with open(file_path, 'r') as file:
  25. text = file.readline().strip()
  26. assert text.strip() != ''
  27. return text
  28. except Exception:
  29. return False
  30. return False