Exemplo n.º 1
0
    def test_with_format(self):
        """bitmath.format context mgr sets and restores formatting"""
        to_print = [
            bitmath.Byte(101),
            bitmath.KiB(202),
            bitmath.MB(303),
            bitmath.GiB(404),
            bitmath.TB(505),
            bitmath.PiB(606),
            bitmath.EB(707)
        ]

        str_reps = [
            "101.00-Byte", "202.00-KiB", "303.00-MB", "404.00-GiB",
            "505.00-TB", "606.00-PiB", "707.00-EB"
        ]

        # Make sure formatting looks right BEFORE the context manager
        self.assertEqual(str(bitmath.KiB(1.337)), "1.337 KiB")

        with bitmath.format("{value:.2f}-{unit}"):
            for (inst, inst_str) in zip(to_print, str_reps):
                self.assertEqual(str(inst), inst_str)

        # Make sure formatting looks right AFTER the context manager
        self.assertEqual(str(bitmath.KiB(1.337)), "1.337 KiB")
Exemplo n.º 2
0
    def test_click_BitmathType_good_one_opt(self):
        @click.command()
        @click.option('--opt', type=BitmathType())
        def func(opt):
            click.echo(opt)

        result = self.runner.invoke(func, ['--opt', '1007TB'])
        self.assertFalse(result.exception)
        self.assertEqual(result.output.splitlines(), [str(bitmath.TB(1007))])
Exemplo n.º 3
0
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])))
Exemplo n.º 4
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)