commons.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. def join_path(*args):
  2. return '/'.join(str(s).rstrip('/') for s in args)
  3. def get_top_percent_list(num_list, percent):
  4. """
  5. 获取列表中前百分之多少的元素
  6. :param num_list:
  7. :param percent:
  8. :return:
  9. """
  10. if len(num_list) == 0:
  11. top_percent_list = []
  12. else:
  13. # 对imgs_len_list排序
  14. sorted_imgs_len_list = sorted(num_list, reverse=True)
  15. # 计算 percent 的索引
  16. top_percent_index = int(len(sorted_imgs_len_list) * percent)
  17. # 取前80%的元素
  18. top_percent_list = sorted_imgs_len_list[:top_percent_index]
  19. return top_percent_list
  20. def mymax(alist: list):
  21. if len(alist) == 0:
  22. return 0 # 空是0, 0*0也是0大小q
  23. else:
  24. return max(alist)
  25. def parse_bucket_key(s3_full_path: str):
  26. """
  27. 输入 s3://bucket/path/to/my/file.txt
  28. 输出 bucket, path/to/my/file.txt
  29. """
  30. s3_full_path = s3_full_path.strip()
  31. if s3_full_path.startswith("s3://"):
  32. s3_full_path = s3_full_path[5:]
  33. if s3_full_path.startswith("/"):
  34. s3_full_path = s3_full_path[1:]
  35. bucket, key = s3_full_path.split("/", 1)
  36. return bucket, key