123456789101112 |
- def parse_bytes(bytes_str):
- conversions = {'B':1, 'KiB':1024, 'MiB':1024**2, 'GiB':1024**3}
- #
- matching_conversions = [x for x in conversions if bytes_str.endswith(x)]
- if len(matching_conversions) > 0:
- # if any conversion suffix matched
- most_precise_match = max(matching_conversions, key=len)
- number = int(bytes_str[:-len(most_precise_match)])
- return number*conversions[most_precise_match]
- #
- return int(bytes_str)
- #
|