Skip to content

Instantly share code, notes, and snippets.

@Caffe1neAdd1ct
Last active October 3, 2017 15:44
Show Gist options
  • Save Caffe1neAdd1ct/2b8f1a51c39e471119663c93f24d33fe to your computer and use it in GitHub Desktop.
Save Caffe1neAdd1ct/2b8f1a51c39e471119663c93f24d33fe to your computer and use it in GitHub Desktop.
Convert Human Readable Sizes to Bytes
def parse_size_to_bytes(size):
"""
Convert Human Readable Sizes to Bytes
https://gist.github.com/Caffe1neAdd1ct/2b8f1a51c39e471119663c93f24d33fe
:param size: String representing size in a human readable format
:return:
"""
units = 'bkmgtpezy'
unit_index = -1
matches = re.search(
"^(?P<size>[0-9?]+)\s?(?P<unit>[{units}]?)".format(units=units),
size,
re.IGNORECASE
)
unit = matches.group('unit').lower().replace(',', '')
size = int(matches.group('size'))
if unit != '':
unit_index = units.index(unit)
if unit_index >= 0:
size = size * pow(1024, unit_index)
return round(size, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment