Created
August 23, 2012 22:02
-
-
Save cjhanks/3442547 to your computer and use it in GitHub Desktop.
Basic SI
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 | |
def __unit(val): | |
if not val[-1:].isalpha(): | |
return val | |
if len(val) <= 1 or val[-2:-1].isalpha(): | |
raise RuntimeError('invalid units') | |
i = int(val[:-1]) | |
char = val[-1:] | |
multiple_table = { | |
'M' : int(10e6), | |
'k' : int(10e3), | |
'h' : int(10e2), | |
} | |
if not char in multiple_table: | |
raise RuntimeError('unsupported unit (%s)' % char) | |
return str(multiple_table[char] * i) | |
print __unit('126k') | |
print __unit('3M') | |
print __unit('4') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment