understanding commited on
Commit
d867b45
Β·
verified Β·
1 Parent(s): 3950a15

Create helpers.py

Browse files
Files changed (1) hide show
  1. utils/helpers.py +34 -0
utils/helpers.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # filename: utils/helpers.py
2
+
3
+ import math
4
+
5
+ def format_bytes(size_bytes: int) -> str:
6
+ """Converts a size in bytes to a human-readable string."""
7
+ if not isinstance(size_bytes, int) or size_bytes <= 0:
8
+ return "0 B"
9
+ size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
10
+ i = min(int(math.log(size_bytes, 1024)), len(size_name) - 1)
11
+ p = math.pow(1024, i)
12
+ s = round(size_bytes / p, 2)
13
+ return f"{s} {size_name[i]}"
14
+
15
+ def create_progress_bar(progress: float) -> str:
16
+ """Creates a text-based progress bar string.
17
+
18
+ Args:
19
+ progress: A float between 0.0 and 1.0.
20
+
21
+ Returns:
22
+ A string representing the progress bar e.g., '[β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘] 50%'
23
+ """
24
+ if not (0.0 <= progress <= 1.0):
25
+ progress = 0.0
26
+
27
+ bar_length = 10
28
+ filled_length = int(bar_length * progress)
29
+
30
+ bar = 'β–ˆ' * filled_length + 'β–‘' * (bar_length - filled_length)
31
+ percentage = f"{progress:.0%}"
32
+
33
+ return f"[{bar}] {percentage}"
34
+