-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_utils.py
33 lines (27 loc) · 1.37 KB
/
file_utils.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
import re
import argparse
def estimate_scale_factor(current_size: int, target_size: int) -> float:
"""Estimate the scale factor to resize an image based on current and target sizes."""
if current_size <= 0:
raise ValueError("Current size must be greater than zero.")
return (target_size / current_size) ** 0.5 # Calculate the scale factor
def parse_size(size_str: str) -> int:
"""Convert a string like '500M' or '12G' to bytes."""
match = re.match(r'^(\d*\.?\d+)\s*([KMGT]?)B?$', size_str, re.I)
if not match:
raise argparse.ArgumentTypeError("Invalid size format. Use something like '500M' or '12G'.")
number, unit = match.groups()
number = float(number) # Convert the number part to float
unit = unit.upper() # Normalize the unit to uppercase
# Define multipliers for each unit
multipliers = {
'K': 1024, # Kilo: 1024 bytes
'M': 1024**2, # Mega: 1024^2 bytes
'G': 1024**3, # Giga: 1024^3 bytes
'T': 1024**4 # Tera: 1024^4 bytes
}
size_in_bytes = int(number * multipliers.get(unit, 1)) # Return the size in bytes
if size_in_bytes <= 0: # Ensure target size is not zero or negative
raise ValueError("Target size must be greater than zero.")
print(f"Parsed target size: {size_str} -> {size_in_bytes} bytes") # Log the parsed size
return size_in_bytes