Last active
March 24, 2017 00:06
-
-
Save drewandersonnz/c7a4613268905866c4d68d41aa6e23ae to your computer and use it in GitHub Desktop.
convert OpenShift text data to numbers in single unit
This file contains 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
#!/usr/bin/python | |
import re | |
def convert_to_bytes(data): | |
(number, unit) = re.search("([0-9]+)([A-Za-z]+)", data.strip()).groups() | |
if unit.lower() == 'bi': | |
return int(number) | |
elif unit.lower() == 'ki': | |
return int(number) * 1024 | |
elif unit.lower() == 'mi': | |
return int(number) * 1024 * 1024 | |
elif unit.lower() == 'gi': | |
return int(number) * 1024 * 1024 * 1024 | |
raise Exception("invalid input data: " + data) | |
def convert_to_kb(data): | |
return float(convert_to_bytes(data)) / 1024 | |
def convert_to_mb(data): | |
return float(convert_to_kb(data)) / 1024 | |
def convert_to_gb(data): | |
return float(convert_to_mb(data)) / 1024 | |
print convert_to_gb("1024Ki") | |
print convert_to_gb("1024Mi") | |
print convert_to_gb("1024Gi") | |
print convert_to_gb("734Mi") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment