def test_parse_unsafe_SI(self): """parse_string_unsafe can parse all accepted SI inputs""" # Begin with the kilo unit because it's the most tricky (SI # defines the unit as a lower-case 'k') kilo_inputs = [ '100k', '100K', '100kb', '100KB', '100kB' ] expected_kilo_result = bitmath.kB(100) for ki in kilo_inputs: _parsed = bitmath.parse_string_unsafe(ki) self.assertEqual(_parsed, expected_kilo_result) self.assertIs(type(_parsed), type(expected_kilo_result)) # Now check for other easier to parse prefixes other_inputs = [ '100g', '100G', '100gb', '100gB', '100GB' ] expected_gig_result = bitmath.GB(100) for gi in other_inputs: _parsed = bitmath.parse_string_unsafe(gi) self.assertEqual(_parsed, expected_gig_result) self.assertIs(type(_parsed), type(expected_gig_result))
def to_base_10(n): if "K" in n[1]: return int(round(bitmath.kB(n[0]).to_Byte())) elif "M" in n[1]: return int(round(bitmath.MB(n[0]).to_Byte())) elif "G" in n[1]: return int(round(bitmath.GB(n[0]).to_Byte())) elif "T" in n[1]: return int(round(bitmath.TB(n[0]).to_Byte())) else: return int(round(float(n[0])))
def setUp(self): self.bit = bitmath.Bit(1) self.byte = bitmath.Byte(1) # NIST units self.kib = bitmath.KiB(1) self.mib = bitmath.MiB(1) self.gib = bitmath.GiB(1) self.tib = bitmath.TiB(1) self.pib = bitmath.PiB(1) self.eib = bitmath.EiB(1) # SI units self.kb = bitmath.kB(1) self.mb = bitmath.MB(1) self.gb = bitmath.GB(1) self.tb = bitmath.TB(1) self.pb = bitmath.PB(1) self.eb = bitmath.EB(1)
def test_simple_round_down(self): """SI: 1 MB (as a GB()) rounds down into a MB()""" # Represent one MB as a small GB MB_in_GB = bitmath.GB(bytes=1048576) # This should turn into a MB self.assertIs(type(MB_in_GB.best_prefix()), bitmath.MB)