Advertisement
mikjaer

Untitled

Oct 31st, 2023
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | Source Code | 0 0
  1. #!/usr/bin/python3
  2. import re
  3. def modifiers(modifier):
  4.     modifiers = {
  5.     "K" : {"M":"1","B":"1000"},
  6.     "M" : {"M":"2","B":"1000"},
  7.     "G" : {"M":"3","B":"1000"},
  8.     "T" : {"M":"4","B":"1000"},
  9.     "P" : {"M":"5","B":"1000"},
  10.  
  11.     "KiB" : {"M":"1","B":"1024"},
  12.     "MiB" : {"M":"2","B":"1024"},
  13.     "GiB" : {"M":"3","B":"1024"},
  14.     "TiB" : {"M":"4","B":"1024"},
  15.     "PiB" : {"M":"5","B":"1024"},
  16.     }
  17.  
  18.     return modifiers[modifier]
  19.  
  20. def convert(input):
  21.     # Removing /s if exists
  22.     if input[-2:] == "/s":  # Removing /s if exists
  23.         input = input[0:-2]
  24.  
  25.     # Getting numeric value and modifier
  26.     m = re.match(r"(?P<numeric>[0-9\.]+)(?P<modifier>.+)", input)
  27.  
  28.     numeric=m.groupdict()["numeric"]
  29.     modifier=m.groupdict()["modifier"]
  30.  
  31.     # Lookup function for modifiers
  32.     m = modifiers(modifier);
  33.  
  34.     # Calculate byte value
  35.     ret = float(numeric)
  36.     for i in range(int(m["M"])):
  37.         ret = ret * int(m["B"])
  38.     ret = int(ret)
  39.  
  40.     return ret
  41.  
  42. for test in ["139MiB/s", "61.8G", "5M", "2TiB/s"]:
  43.     print (" - ",test," B:", convert(test))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement