Skip to content

Instantly share code, notes, and snippets.

@drewandersonnz
Last active March 24, 2017 00:06
Show Gist options
  • Save drewandersonnz/c7a4613268905866c4d68d41aa6e23ae to your computer and use it in GitHub Desktop.
Save drewandersonnz/c7a4613268905866c4d68d41aa6e23ae to your computer and use it in GitHub Desktop.
convert OpenShift text data to numbers in single unit
#!/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