Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import re
- def modifiers(modifier):
- modifiers = {
- "K" : {"M":"1","B":"1000"},
- "M" : {"M":"2","B":"1000"},
- "G" : {"M":"3","B":"1000"},
- "T" : {"M":"4","B":"1000"},
- "P" : {"M":"5","B":"1000"},
- "KiB" : {"M":"1","B":"1024"},
- "MiB" : {"M":"2","B":"1024"},
- "GiB" : {"M":"3","B":"1024"},
- "TiB" : {"M":"4","B":"1024"},
- "PiB" : {"M":"5","B":"1024"},
- }
- return modifiers[modifier]
- def convert(input):
- # Removing /s if exists
- if input[-2:] == "/s": # Removing /s if exists
- input = input[0:-2]
- # Getting numeric value and modifier
- m = re.match(r"(?P<numeric>[0-9\.]+)(?P<modifier>.+)", input)
- numeric=m.groupdict()["numeric"]
- modifier=m.groupdict()["modifier"]
- # Lookup function for modifiers
- m = modifiers(modifier);
- # Calculate byte value
- ret = float(numeric)
- for i in range(int(m["M"])):
- ret = ret * int(m["B"])
- ret = int(ret)
- return ret
- for test in ["139MiB/s", "61.8G", "5M", "2TiB/s"]:
- print (" - ",test," B:", convert(test))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement