Skip to content

Instantly share code, notes, and snippets.

@cjhanks
Created August 23, 2012 22:02
Show Gist options
  • Save cjhanks/3442547 to your computer and use it in GitHub Desktop.
Save cjhanks/3442547 to your computer and use it in GitHub Desktop.
Basic SI
#!/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