MCPcopy
hub / github.com/corpnewt/GenSMBIOS / get_size

Function get_size

Scripts/downloader.py:15–47  ·  view source on GitHub ↗
(size, suffix=None, use_1024=False, round_to=2, strip_zeroes=False)

Source from the content-addressed store, hash-verified

13TERMINAL_WIDTH = 120 if os.name=="nt" else 80
14
15def get_size(size, suffix=None, use_1024=False, round_to=2, strip_zeroes=False):
16 # size is the number of bytes
17 # suffix is the target suffix to locate (B, KB, MB, etc) - if found
18 # use_2014 denotes whether or not we display in MiB vs MB
19 # round_to is the number of dedimal points to round our result to (0-15)
20 # strip_zeroes denotes whether we strip out zeroes
21
22 # Failsafe in case our size is unknown
23 if size == -1:
24 return "Unknown"
25 # Get our suffixes based on use_1024
26 ext = ["B","KiB","MiB","GiB","TiB","PiB"] if use_1024 else ["B","KB","MB","GB","TB","PB"]
27 div = 1024 if use_1024 else 1000
28 s = float(size)
29 s_dict = {} # Initialize our dict
30 # Iterate the ext list, and divide by 1000 or 1024 each time to setup the dict {ext:val}
31 for e in ext:
32 s_dict[e] = s
33 s /= div
34 # Get our suffix if provided - will be set to None if not found, or if started as None
35 suffix = next((x for x in ext if x.lower() == suffix.lower()),None) if suffix else suffix
36 # Get the largest value that's still over 1
37 biggest = suffix if suffix else next((x for x in ext[::-1] if s_dict[x] >= 1), "B")
38 # Determine our rounding approach - first make sure it's an int; default to 2 on error
39 try:round_to=int(round_to)
40 except:round_to=2
41 round_to = 0 if round_to < 0 else 15 if round_to > 15 else round_to # Ensure it's between 0 and 15
42 bval = round(s_dict[biggest], round_to)
43 # Split our number based on decimal points
44 a,b = str(bval).split(".")
45 # Check if we need to strip or pad zeroes
46 b = b.rstrip("0") if strip_zeroes else b.ljust(round_to,"0") if round_to > 0 else ""
47 return "{:,}{} {}".format(int(a),"" if not b else "."+b,biggest)
48
49def _process_hook(queue, total_size, bytes_so_far=0, update_interval=1.0, max_packets=0):
50 packets = []

Callers 2

_process_hookFunction · 0.85
get_sizeMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected