useful.py 429 B

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