utils.py 588 B

1234567891011121314151617181920212223242526
  1. import os
  2. import sys
  3. import threading
  4. class ProgressPercentage(object):
  5. def __init__(self, filename):
  6. self._filename = filename
  7. self._size = float(os.path.getsize(filename))
  8. self._seen_so_far = 0
  9. self._lock = threading.Lock()
  10. def __call__(self, bytes_amount):
  11. # To simplify, assume this is hooked up to a single filename
  12. with self._lock:
  13. self._seen_so_far += bytes_amount
  14. percentage = (self._seen_so_far / self._size) * 100
  15. sys.stdout.write(
  16. "\r%s / %s (%.2f%%)\n" % (
  17. self._seen_so_far, self._size,
  18. percentage
  19. )
  20. )
  21. sys.stdout.flush()