Python bitmath.Byte() Examples

The following are 30 code examples of bitmath.Byte(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module bitmath , or try the search function .
Example #1
Source File: _textrenderer.py    From kubetop with MIT License 6 votes vote down vote up
def _pod_stats(pod):
    cpu = sum(
        map(
            parse_cpu, (
                container["usage"]["cpu"]
                for container
                in pod["containers"]
            ),
        ), 0,
    )
    mem = sum(
        map(
            lambda s: parse_memory(s).amount, (
                container["usage"]["memory"]
                for container
                in pod["containers"]
            ),
        ), Byte(0),
    )
    return (_CPU(cpu), _Memory(mem)) 
Example #2
Source File: test_ebs.py    From flocker with Apache License 2.0 6 votes vote down vote up
def test_foreign_volume(self):
        """
        ``list_volumes`` lists only those volumes
        belonging to the current Flocker cluster.
        """
        try:
            config = get_blockdevice_config()
        except InvalidConfig as e:
            self.skipTest(str(e))
        ec2_client = get_ec2_client_for_test(config)
        meta_client = ec2_client.connection.meta.client
        requested_volume = meta_client.create_volume(
            Size=int(Byte(self.minimum_allocatable_size).to_GiB().value),
            AvailabilityZone=ec2_client.zone)
        created_volume = ec2_client.connection.Volume(
            requested_volume['VolumeId'])
        self.addCleanup(created_volume.delete)

        _wait_for_volume_state_change(VolumeOperations.CREATE,
                                      created_volume)

        self.assertEqual(self.api.list_volumes(), []) 
Example #3
Source File: test_to_Type_conversion.py    From bitmath with MIT License 6 votes vote down vote up
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)
        self.zb = bitmath.ZB(1)
        self.yb = bitmath.YB(1) 
Example #4
Source File: test_api.py    From flocker with Apache License 2.0 6 votes vote down vote up
def test_parsenum_all_sizes(self, expr, size):
        """
        Send standard size expressions to ``parse_num`` in
        many sizes, we expect to get correct size results.

        :param expr str: A string representing the size expression
        :param size int: A string representing the volume size
        """
        if expr is "KB":
            expected_size = int(KiB(size).to_Byte())
        elif expr is "MB":
            expected_size = int(MiB(size).to_Byte())
        elif expr is "GB":
            expected_size = int(GiB(size).to_Byte())
        elif expr is "TB":
            expected_size = int(TiB(size).to_Byte())
        else:
            expected_size = int(Byte(size).to_Byte())
        return self.assertEqual(expected_size,
                                int(parse_num(str(size)+expr).to_Byte())) 
Example #5
Source File: test_init.py    From bitmath with MIT License 6 votes vote down vote up
def test___init__valid_inputs(self):
        """__init__: accepts valid inputs"""
        inputs = [
            # Comments illustrate what the initialization calls look
            # like after the interpreter expands all the *arg/**kwarg
            # parameters.
            #
            # All pairs are equivalent to Byte(100) (used in the test
            # assertion, below)
            ((100,), dict()),  # Byte(100)
            (tuple(), {"value": 100}),  # Byte(value=100)
            (tuple(), {"bytes": 100}),  # Byte(bytes=100)
            (tuple(), {"bits": 800})  # Byte(bits=800)
        ]

        for args, kwargs in inputs:
            self.assertEqual(bitmath.Byte(*args, **kwargs), bitmath.Byte(100)) 
Example #6
Source File: errors.py    From quay with Apache License 2.0 6 votes vote down vote up
def __init__(self, uploaded=None, max_allowed=None):
        detail = {}
        message = "Uploaded blob is larger than allowed by this registry"

        if uploaded is not None and max_allowed is not None:
            detail = {
                "reason": "%s is greater than maximum allowed size %s" % (uploaded, max_allowed),
                "max_allowed": max_allowed,
                "uploaded": uploaded,
            }

            up_str = bitmath.Byte(uploaded).best_prefix().format("{value:.2f} {unit}")
            max_str = bitmath.Byte(max_allowed).best_prefix().format("{value:.2f} {unit}")
            message = "Uploaded blob of %s is larger than %s allowed by this registry" % (
                up_str,
                max_str,
            ) 
Example #7
Source File: test_best_prefix_BASE.py    From bitmath with MIT License 6 votes vote down vote up
def test_best_prefix_negative_less_than_a_byte(self):
        """best_prefix_base: negative values less than a byte stay as bits"""
        # assert that a Byte of -4 bits yields Bit(-4)
        bm1 = bitmath.Byte(bits=-4)
        expected = bitmath.Bit(-4)
        res = bitmath.best_prefix(bm1)
        # Verify that best prefix math works for negative numbers
        self.assertEqual(res, expected)
        # Verify that best prefix guessed the correct type
        self.assertIs(type(res), bitmath.Bit)

    # For instances where x in set { b | b >= 8 } where b is number of
    # bits in an instance:
    #
    # * bitmath.best_prefix(-10**8) -> MiB(-95.367...)
    # * bitmath.best_prefix(10**8) -> MiB(95.367...) 
Example #8
Source File: test_textrenderer.py    From kubetop with MIT License 6 votes vote down vote up
def test_render_pod(self):
        pod_usage = {
            "metadata": {
                "name": "foo",
                "namespace": "default",
                "creationTimestamp": "2017-04-07T15:21:22Z"
            },
            "timestamp": "2017-04-07T15:21:00Z",
            "window": "1m0s",
            "containers": [
                {
                    "name": "foo-a",
                    "usage": {
                        "cpu": "100m",
                        "memory": "128Ki"
                    }
                },
            ]
        }
        fields = _render_pod(pod_usage, _Memory(Byte(1024 * 1024))).split()
        self.assertEqual(
            [u'foo', u'10.0', u'128.00', u'KiB', u'12.50'],
            fields,
        ) 
Example #9
Source File: test_textrenderer.py    From kubetop with MIT License 5 votes vote down vote up
def test_bytes(self):
        self.assertEqual(
            "  123.00 Byte",
            _Memory(Byte(123)).render("8.2"),
        ) 
Example #10
Source File: test_textrenderer.py    From kubetop with MIT License 5 votes vote down vote up
def test_kibibytes(self):
        self.assertEqual(
            "   12.50 KiB",
            _Memory(Byte(1024 * 12 + 512)).render("8.2"),
        ) 
Example #11
Source File: test_cinder.py    From flocker with Apache License 2.0 5 votes vote down vote up
def test_disk_attachment_fails_with_conflicting_disk(self):
        """
        create_server_volume will raise an exception when Cinder attempts to
        attach a device to a path that is in use by a non-Cinder volume.
        """
        instance_id = self.blockdevice_api.compute_instance_id()

        host_device = "/dev/null"
        tmpdir = FilePath(self.mktemp())
        tmpdir.makedirs()
        virtio = VirtIOClient.using_insecure_tls(instance_id, tmpdir)
        virtio.attach_disk(host_device, "vdb")
        self.addCleanup(virtio.detach_disk, host_device)

        cinder_volume = self.cinder.create(
            size=int(Byte(get_minimum_allocatable_size()).to_GiB().value)
        )
        CINDER_VOLUME(id=cinder_volume.id).write()
        self.addCleanup(self._cleanup, instance_id, cinder_volume)
        volume = wait_for_volume_state(
            volume_manager=self.cinder, expected_volume=cinder_volume,
            desired_state=u'available', transient_states=(u'creating',))

        attached_volume = self.nova.create_server_volume(
            server_id=instance_id,
            volume_id=volume.id,
            device=None,
        )

        with self.assertRaises(UnexpectedStateException) as e:
            wait_for_volume_state(
                volume_manager=self.cinder,
                expected_volume=attached_volume,
                desired_state=u'in-use',
                transient_states=(u'available', u'attaching',),
            )
        self.assertEqual(e.exception.unexpected_state, u'available') 
Example #12
Source File: test_textrenderer.py    From kubetop with MIT License 5 votes vote down vote up
def test_mebibytes(self):
        self.assertEqual(
            "  123.25 MiB",
            _Memory(Byte(2 ** 20 * 123 + 2 ** 20 / 4)).render("8.2"),
        ) 
Example #13
Source File: test_textrenderer.py    From kubetop with MIT License 5 votes vote down vote up
def test_tebibytes(self):
        self.assertEqual(
            "  100.00 TiB",
            _Memory(Byte(2 ** 40 * 100)).render("8.2"),
        ) 
Example #14
Source File: test_cinder.py    From flocker with Apache License 2.0 5 votes vote down vote up
def test_get_device_path_virtio_blk_symlink(self):
        """
        ``get_device_path`` on systems using the virtio_blk driver
        returns the target of a symlink matching
        ``/dev/disks/by-id/virtio-<volume.id>``.
        """
        instance_id = self.blockdevice_api.compute_instance_id()
        # Create volume
        cinder_volume = self.cinder.create(
            size=int(Byte(get_minimum_allocatable_size()).to_GiB().value)
        )
        CINDER_VOLUME(id=cinder_volume.id).write()
        self.addCleanup(self._cleanup, instance_id, cinder_volume)
        volume = wait_for_volume_state(
            volume_manager=self.cinder,
            expected_volume=cinder_volume,
            desired_state=u'available',
            transient_states=(u'creating',))

        # Attach volume
        attached_volume = self.nova.create_server_volume(
            server_id=instance_id,
            volume_id=volume.id,
            device=None,
        )
        volume = wait_for_volume_state(
            volume_manager=self.cinder,
            expected_volume=attached_volume,
            desired_state=u'in-use',
            transient_states=(u'available', u'attaching',),
        )
        self.assertEqual(
            FilePath(
                '/dev/disk/by-id/virtio-{}'.format(volume.id[:20])
            ).realpath(),
            self.blockdevice_api.get_device_path(
                volume.id
            )
        ) 
Example #15
Source File: test_blockdevice.py    From flocker with Apache License 2.0 5 votes vote down vote up
def _make_allocated_size_testcases():
    """
    Build test cases for some common allocation_units.
    """
    for unit in (Byte, MB, MiB, GB, GiB):
        for size in (1, 2, 4, 8):
            test_case = make_allocated_size_tests(unit(size))
            globals()[test_case.__name__] = test_case 
Example #16
Source File: gce.py    From flocker with Apache License 2.0 5 votes vote down vote up
def create_volume_with_profile(self, dataset_id, size, profile_name):
        blockdevice_id = _dataset_id_to_blockdevice_id(dataset_id)
        size = Byte(size)
        profile_type = MandatoryProfiles.lookupByValue(profile_name).name
        gce_disk_type = GCEStorageProfiles.lookupByName(profile_type).value
        try:
            self._operations.create_disk(
                name=blockdevice_id,
                size=size,
                description=self._disk_resource_description(),
                gce_disk_type=gce_disk_type
            )
        except HttpError as e:
            if e.resp.status == 409:
                msg = ("A dataset named {} already exists in this GCE "
                       "project.".format(dataset_id))
                raise GCEVolumeException(msg)
            else:
                raise

        disk = self._operations.get_disk_details(blockdevice_id)
        return BlockDeviceVolume(
            blockdevice_id=blockdevice_id,
            size=int(GiB(int(disk['sizeGb'])).to_Byte()),
            attached_to=_extract_attached_to(disk),
            dataset_id=_blockdevice_id_to_dataset_id(blockdevice_id),
        ) 
Example #17
Source File: test_textrenderer.py    From kubetop with MIT License 5 votes vote down vote up
def test_pebibytes(self):
        self.assertEqual(
            "  100.00 PiB",
            _Memory(Byte(2 ** 50 * 100)).render("8.2"),
        ) 
Example #18
Source File: test_textrenderer.py    From kubetop with MIT License 5 votes vote down vote up
def test_exbibytes(self):
        self.assertEqual(
            "  100.00 EiB",
            _Memory(Byte(2 ** 60 * 100)).render("8.2"),
        ) 
Example #19
Source File: test_parse.py    From bitmath with MIT License 5 votes vote down vote up
def test_parse_string_unsafe_request_NIST(self):
        """parse_string_unsafe can convert to NIST on request"""
        unsafe_input = "100M"
        _parsed = bitmath.parse_string_unsafe(unsafe_input, system=bitmath.NIST)
        expected = bitmath.MiB(100)

        self.assertEqual(_parsed, expected)
        self.assertIs(type(_parsed), type(expected))

        unsafe_input2 = "100k"
        _parsed2 = bitmath.parse_string_unsafe(unsafe_input2, system=bitmath.NIST)
        expected2 = bitmath.KiB(100)

        self.assertEqual(_parsed2, expected2)
        self.assertIs(type(_parsed2), type(expected2))

        unsafe_input3 = "100"
        _parsed3 = bitmath.parse_string_unsafe(unsafe_input3, system=bitmath.NIST)
        expected3 = bitmath.Byte(100)

        self.assertEqual(_parsed3, expected3)
        self.assertIs(type(_parsed3), type(expected3))

        unsafe_input4 = "100kb"
        _parsed4 = bitmath.parse_string_unsafe(unsafe_input4, system=bitmath.NIST)
        expected4 = bitmath.KiB(100)

        self.assertEqual(_parsed4, expected4)
        self.assertIs(type(_parsed4), type(expected4))

    ###################################################################### 
Example #20
Source File: _textrenderer.py    From kubetop with MIT License 5 votes vote down vote up
def parse_memory(s):
    return _Memory(Byte(parse_k8s_resource(s, default_scale=1))) 
Example #21
Source File: test_cinder_behaviour.py    From flocker with Apache License 2.0 5 votes vote down vote up
def test_updated_metadata_is_listed(self):
        """
        ``metadata`` supplied to update_metadata is included when that
        volume is subsequently listed.
        """
        expected_metadata = {random_name(self): u"bar"}

        new_volume = self.cinder_volumes.create(
            size=int(Byte(get_minimum_allocatable_size()).to_GiB().value),
        )
        CINDER_VOLUME(id=new_volume.id).write()
        self.addCleanup(self.cinder_volumes.delete, new_volume)

        listed_volume = wait_for_volume_state(
            volume_manager=self.cinder_volumes,
            expected_volume=new_volume,
            desired_state=u'available',
            transient_states=(u'creating',),
        )

        self.cinder_volumes.set_metadata(new_volume, expected_metadata)

        listed_volume = wait_for_volume_state(
            volume_manager=self.cinder_volumes,
            expected_volume=new_volume,
            desired_state=u'available',
        )

        expected_items = set(expected_metadata.items())
        actual_items = set(listed_volume.metadata.items())
        missing_items = expected_items - actual_items

        self.assertEqual(set(), missing_items) 
Example #22
Source File: test_cinder_behaviour.py    From flocker with Apache License 2.0 5 votes vote down vote up
def test_create_metadata_is_listed(self):
        """
        ``metadata`` supplied when creating a volume is included when that
        volume is subsequently listed.
        """
        expected_metadata = {random_name(self): "bar"}

        new_volume = self.cinder_volumes.create(
            size=int(Byte(get_minimum_allocatable_size()).to_GiB().value),
            metadata=expected_metadata
        )
        CINDER_VOLUME(id=new_volume.id).write()
        self.addCleanup(self.cinder_volumes.delete, new_volume)
        listed_volume = wait_for_volume_state(
            volume_manager=self.cinder_volumes,
            expected_volume=new_volume,
            desired_state=u'available',
            transient_states=(u'creating',),
        )

        expected_items = set(expected_metadata.items())
        actual_items = set(listed_volume.metadata.items())
        missing_items = expected_items - actual_items
        self.assertEqual(
            set(), missing_items,
            'Metadata {!r} does not contain the expected items {!r}'.format(
                actual_items, expected_items
            )
        ) 
Example #23
Source File: _api.py    From flocker with Apache License 2.0 5 votes vote down vote up
def parse_num(expression):
    """
    Parse a string of a dataset size 10g, 100kib etc into
    a usable integer.
    If user doesn't submit a correct size, give back
    the default size.

    :param expression: the dataset expression to parse.
    """
    if not expression:
        return DEFAULT_SIZE
    if type(expression) is unicode:
        expression = str(expression)

    def _match(exp, search=re.compile(
            r'^(\d+){1}([KMGTkmgt][IiBb]){0,1}([Bb]){0,1}').search):
        return bool(search(exp))

    if _match(expression):
        unit = expression.translate(None, "1234567890.")
        num = int(expression.replace(unit, ""))
        unit = unit.lower()
        if unit == 'tb' or unit == 't' or unit == 'tib':
            return TiB(num)
        elif unit == 'gb' or unit == 'g' or unit == 'gib':
            return GiB(num)
        elif unit == 'mb' or unit == 'm' or unit == 'mib':
            return MiB(num)
        elif unit == 'kb' or unit == 'k' or unit == 'kib':
            return KiB(num)
        elif unit == '':
            return Byte(num)
        else:
            return DEFAULT_SIZE
    else:
        return DEFAULT_SIZE 
Example #24
Source File: test_basic_math.py    From bitmath with MIT License 5 votes vote down vote up
def bitmath_add_bitmath_is_bitmath(self):
        """bitmath + bitmath = bitmath"""
        bm1 = bitmath.KiB(1)
        bm2 = bitmath.KiB(2)
        result = bm1 + bm2
        self.assertEqual(result, bitmath.KiB(3))
        self.assertIs(type(result), bitmath.Byte) 
Example #25
Source File: test_basic_math.py    From bitmath with MIT License 5 votes vote down vote up
def test_different_bitmath_types_equality(self):
        """Two different bitmath types are equal"""
        self.assertEqual(bitmath.KiB(1), bitmath.Byte(self.kib_in_bytes)) 
Example #26
Source File: test_context_manager.py    From bitmath with MIT License 5 votes vote down vote up
def test_print_GiB_plural_fmt_in_mgr(self):
        """TiB(1/3.0) prints out units in plural form, setting the fmt str in the mgr"""
        expected_result = "3Bytes"

        with bitmath.format(fmt_str="{value:.1g}{unit}", plural=True):
            three_Bytes = bitmath.Byte(3.0)
            actual_result = str(three_Bytes)
            self.assertEqual(expected_result, actual_result) 
Example #27
Source File: test_context_manager.py    From bitmath with MIT License 5 votes vote down vote up
def test_print_byte_plural_fmt_in_mgr(self):
        """Byte(3.0) prints out units in plural form, setting the fmt str in the mgr"""
        expected_result = "3Bytes"

        with bitmath.format(fmt_str="{value:.1g}{unit}", plural=True):
            three_Bytes = bitmath.Byte(3.0)
            actual_result = str(three_Bytes)
            self.assertEqual(expected_result, actual_result) 
Example #28
Source File: test_context_manager.py    From bitmath with MIT License 5 votes vote down vote up
def test_print_byte_plural(self):
        """Byte(3.0) prints out units in plural form"""
        expected_result = "3Bytes"
        fmt_str = "{value:.1g}{unit}"
        three_Bytes = bitmath.Byte(3.0)

        with bitmath.format(plural=True):
            actual_result = three_Bytes.format(fmt_str)
            self.assertEqual(expected_result, actual_result) 
Example #29
Source File: test_context_manager.py    From bitmath with MIT License 5 votes vote down vote up
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") 
Example #30
Source File: __init__.py    From bitmath with MIT License 5 votes vote down vote up
def unit_plural(self):
        """The string that is an instances prefix unit name in the plural
form.

For example:

   >>> KiB(1).unit_plural == 'KiB'
   >>> Byte(1024).unit_plural == 'Bytes'
   >>> Gb(1).unit_plural == 'Gb'

        """
        return self._name_plural