Last active
October 3, 2017 15:44
-
-
Save Caffe1neAdd1ct/2b8f1a51c39e471119663c93f24d33fe to your computer and use it in GitHub Desktop.
Convert Human Readable Sizes to Bytes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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