DownloadProgress.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # -*- coding: utf-8 -*-
  2. '''
  3. 下载进度
  4. @Auther :liuyuqi.gov@msn.cn
  5. @date :2019/4/8
  6. '''
  7. __author__ = "liuyuqi"
  8. class DownloadProgress(object):
  9. def __init__(self, title, count=0.0, run_status=None, fin_status=None, total=100.0, unit='', sep='/',
  10. chunk_size=1.0):
  11. super(DownloadProgress, self).__init__()
  12. self.info = "[%s] %s %.2f %s %s %.2f %s"
  13. self.title = title
  14. self.total = total
  15. self.count = count
  16. self.chunk_size = chunk_size
  17. self.status = run_status or ""
  18. self.fin_status = fin_status or " " * len(self.status)
  19. self.unit = unit
  20. self.seq = sep
  21. def __get_info(self):
  22. # 【名称】状态 进度 单位 分割线 总数 单位
  23. _info = self.info % (
  24. self.title, self.status, self.count / self.chunk_size, self.unit, self.seq, self.total / self.chunk_size,
  25. self.unit)
  26. return _info
  27. def refresh(self, count=1, status=None):
  28. self.count += count
  29. # if status is not None:
  30. self.status = status or self.status
  31. end_str = "\r"
  32. if self.count >= self.total:
  33. end_str = '\n'
  34. self.status = status or self.fin_status
  35. print(self.__get_info(), end=end_str)