-
Notifications
You must be signed in to change notification settings - Fork 1
/
progress_bar.py
59 lines (40 loc) · 1.8 KB
/
progress_bar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
import sys
import threading
class ProgressPercentage(object):
"""
Script will track the uploading progress of each data file being transferred to cloud data storage.
"""
def __init__(self, file_path):
"""
Args:
file_path (str): Data file's full directory path (incl. filename).
"""
# Data file's full directory path (incl. filename).
self.file_path = file_path
# File's size.
self.size = float(os.path.getsize(file_path))
# Initialize filesize counter, which details the file size that has already been
# uploaded at any given time.
self.seen_so_far = 0
# Lock worker threads to prevent losing the worker threads during file processing.
self.lock = threading.Lock()
def __call__(self, bytes_amount):
"""
Establish an uploading progress bar.
Args:
bytes_amount (float): Bytes that have already been transferred to the cloud data storage
for the given file.
Return: None
"""
# Once worker threads are locked, cumulate filesize counter.
with self.lock:
# Cumulative file sizes in bytes.
self.seen_so_far += bytes_amount
# Percentage of the file uploading progress.
percentage = (self.seen_so_far / self.size) * 100
# Display progress bar.
sys.stdout.write("\r%s %s / %s (%.2f%%)" % (self.file_path, self.seen_so_far, self.size, percentage))
# Return system resource back to memory.
sys.stdout.flush()
return