當前位置: 首頁>>代碼示例>>Python>>正文


Python size.Size類代碼示例

本文整理匯總了Python中blivet.size.Size的典型用法代碼示例。如果您正苦於以下問題:Python Size類的具體用法?Python Size怎麽用?Python Size使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Size類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: testResize

    def testResize(self):
        an_fs = self._fs_class()
        if not an_fs.formattable:
            self.skipTest("can not create filesystem %s" % an_fs.name)
        an_fs.device = self.loopDevices[0]
        self.assertIsNone(an_fs.create())
        an_fs.updateSizeInfo()

        self._test_sizes(an_fs)
        # CHECKME: target size is still 0 after updatedSizeInfo is called.
        self.assertEqual(an_fs.size, Size(0) if an_fs.resizable else an_fs._size)

        if not can_resize(an_fs):
            self.assertFalse(an_fs.resizable)
            # Not resizable, so can not do resizing actions.
            with self.assertRaises(FSError):
                an_fs.targetSize = Size("64 MiB")
            with self.assertRaises(FSError):
                an_fs.doResize()
        else:
            self.assertTrue(an_fs.resizable)
            # Try a reasonable target size
            TARGET_SIZE = Size("64 MiB")
            an_fs.targetSize = TARGET_SIZE
            self.assertEqual(an_fs.targetSize, TARGET_SIZE)
            self.assertNotEqual(an_fs._size, TARGET_SIZE)
            self.assertIsNone(an_fs.doResize())
            ACTUAL_SIZE = TARGET_SIZE.roundToNearest(an_fs._resize.unit, rounding=ROUND_DOWN)
            self.assertEqual(an_fs.size, ACTUAL_SIZE)
            self.assertEqual(an_fs._size, ACTUAL_SIZE)
            self._test_sizes(an_fs)

        # and no errors should occur when checking
        self.assertIsNone(an_fs.doCheck())
開發者ID:cgwalters,項目名稱:blivet,代碼行數:34,代碼來源:fstesting.py

示例2: testShrink

    def testShrink(self):
        an_fs = self._fs_class()
        if not can_resize(an_fs):
            self.skipTest("Not checking resize for this test category.")
        if not an_fs.formattable:
            self.skipTest("can not create filesystem %s" % an_fs.name)

        an_fs.device = self.loopDevices[0]
        self.assertIsNone(an_fs.create())
        an_fs.updateSizeInfo()

        TARGET_SIZE = Size("64 MiB")
        an_fs.targetSize = TARGET_SIZE
        self.assertIsNone(an_fs.doResize())

        TARGET_SIZE = TARGET_SIZE / 2
        self.assertTrue(TARGET_SIZE > an_fs.minSize)
        an_fs.targetSize = TARGET_SIZE
        self.assertEqual(an_fs.targetSize, TARGET_SIZE)
        self.assertNotEqual(an_fs._size, TARGET_SIZE)
        # FIXME:
        # doCheck() in updateSizeInfo() in doResize() does not complete tidily
        # here, so resizable becomes False and self.targetSize can not be
        # assigned to. This alerts us to the fact that now min size
        # and size are both incorrect values.
        if isinstance(an_fs, fs.NTFS):
            return
        self.assertIsNone(an_fs.doResize())
        ACTUAL_SIZE = TARGET_SIZE.roundToNearest(an_fs._resize.unit, rounding=ROUND_DOWN)
        self.assertEqual(an_fs._size, ACTUAL_SIZE)
        self._test_sizes(an_fs)
開發者ID:cgwalters,項目名稱:blivet,代碼行數:31,代碼來源:fstesting.py

示例3: testExceptions

    def testExceptions(self):
        zero = Size(0)
        self.assertEqual(zero, 0.0)

        s = Size(500)
        self.assertRaises(SizePlacesError, s.humanReadable, places=-1)

        self.assertEqual(s.humanReadable(places=0), "500 B")
開發者ID:ryanlerch,項目名稱:blivet,代碼行數:8,代碼來源:size_test.py

示例4: testExceptions

    def testExceptions(self):
        zero = Size(0)
        self.assertEqual(zero, Size("0.0"))

        s = Size(500)
        with self.assertRaises(SizePlacesError):
            s.humanReadable(max_places=-1)

        self.assertEqual(s.humanReadable(max_places=0), "500 B")
開發者ID:dashea,項目名稱:blivet,代碼行數:9,代碼來源:size_test.py

示例5: testHumanReadableTranslation

    def testHumanReadableTranslation(self):
        s = Size("56.19 MiB")
        size_str = s.humanReadable()
        for lang in self.TEST_LANGS:

            os.environ['LANG'] = lang
            locale.setlocale(locale.LC_ALL, '')
            self.assertTrue(s.humanReadable().endswith("%s%s" % (_("Mi"), _("B"))))
            self.assertEqual(s.humanReadable(xlate=False), size_str)
開發者ID:dashea,項目名稱:blivet,代碼行數:9,代碼來源:size_test.py

示例6: test_exceptions

    def test_exceptions(self):
        zero = Size(0)
        self.assertEqual(zero, Size("0.0"))

        s = Size(500)
        with self.assertRaises(ValueError):
            s.human_readable(max_places="2")

        self.assertEqual(s.human_readable(max_places=0), "500 B")
開發者ID:AdamWill,項目名稱:blivet,代碼行數:9,代碼來源:size_test.py

示例7: testExceptions

    def testExceptions(self):
        self.assertRaises(SizeParamsError, Size)
        self.assertRaises(SizeParamsError, Size, bytes=500, spec="45GB")

        zero = Size(bytes=0)
        self.assertEqual(zero, 0.0)

        s = Size(bytes=500)
        self.assertRaises(SizePlacesError, s.humanReadable, places=-1)

        self.assertEqual(s.humanReadable(places=0), "500 B")
開發者ID:Sabayon,項目名稱:blivet,代碼行數:11,代碼來源:size_test.py

示例8: testConvertToWithSize

    def testConvertToWithSize(self):
        s = Size(1835008)
        self.assertEqual(s.convertTo(Size(1)), s.convertTo(B))
        self.assertEqual(s.convertTo(Size(1024)), s.convertTo(KiB))
        self.assertEqual(Size(512).convertTo(Size(1024)), Decimal("0.5"))
        self.assertEqual(Size(1024).convertTo(Size(512)), Decimal(2))

        with self.assertRaises(ValueError):
            s.convertTo(Size(0))
開發者ID:dashea,項目名稱:blivet,代碼行數:9,代碼來源:size_test.py

示例9: testMinValue

    def testMinValue(self):
        s = Size("9 MiB")
        self.assertEqual(s.humanReadable(), "9 MiB")
        self.assertEqual(s.humanReadable(min_value=10), "9216 KiB")

        s = Size("0.5 GiB")
        self.assertEqual(s.humanReadable(max_places=2, min_value=1), "512 MiB")
        self.assertEqual(s.humanReadable(max_places=2, min_value=Decimal("0.1")), "0.5 GiB")
        self.assertEqual(s.humanReadable(max_places=2, min_value=Decimal(1)), "512 MiB")
開發者ID:dashea,項目名稱:blivet,代碼行數:9,代碼來源:size_test.py

示例10: _prefixTestHelper

    def _prefixTestHelper(self, numunits, unit):
        """ Test that units and prefix or abbreviation agree.

            :param int numunits: this value times factor yields number of bytes
            :param unit: a unit specifier
        """
        c = numunits * unit.factor

        s = Size(c)
        self.assertEqual(s, Size(c))

        u = size._makeSpec(unit.prefix, size._BYTES_WORDS[0], False)
        s = Size("%ld %s" % (numunits, u))
        self.assertEqual(s, c)
        self.assertEqual(s.convertTo(unit), numunits)

        u = size._makeSpec(unit.abbr, size._BYTES_SYMBOL, False)
        s = Size("%ld %s" % (numunits, u))
        self.assertEqual(s, c)
        self.assertEqual(s.convertTo(unit), numunits)
開發者ID:dashea,項目名稱:blivet,代碼行數:20,代碼來源:size_test.py

示例11: size_from_input

def size_from_input(input_str):
    """Get size from user's input"""

    if not input_str:
        # Nothing to parse
        return None

    # if no unit was specified, default to MiB. Assume that a string
    # ending with anything other than a digit has a unit suffix
    if re.search(r'[\d.%s]$' % locale.nl_langinfo(locale.RADIXCHAR), input_str):
        input_str += "MiB"

    try:
        size = Size(spec=input_str)
    except (SizeParamsError, ValueError):
        return None
    else:
        # Minimium size for ui-created partitions is 1MiB.
        if size.convertTo(spec="MiB") < 1:
            size = Size(spec="1 MiB")

    return size
開發者ID:akozumpl,項目名稱:anaconda,代碼行數:22,代碼來源:storage_utils.py

示例12: _prefixTestHelper

    def _prefixTestHelper(self, numbytes, factor, prefix, abbr):
        c = numbytes * factor

        s = Size(c)
        self.assertEquals(s, c)

        if prefix:
            u = "%sbytes" % prefix
            s = Size("%ld %s" % (numbytes, u))
            self.assertEquals(s, c)
            self.assertEquals(s.convertTo(spec=u), numbytes)

        if abbr:
            u = "%sb" % abbr
            s = Size("%ld %s" % (numbytes, u))
            self.assertEquals(s, c)
            self.assertEquals(s.convertTo(spec=u), numbytes)

        if not prefix and not abbr:
            s = Size("%ld" % numbytes)
            self.assertEquals(s, c)
            self.assertEquals(s.convertTo(), numbytes)
開發者ID:ryanlerch,項目名稱:blivet,代碼行數:22,代碼來源:size_test.py

示例13: testHumanReadableFractionalQuantities

    def testHumanReadableFractionalQuantities(self):
        s = Size(0xfffffffffffff)
        self.assertEqual(s.humanReadable(max_places=2), "4 PiB")
        s = Size(0xfffff)
        self.assertEqual(s.humanReadable(max_places=2, strip=False), "1024.00 KiB")
        s = Size(0xffff)
        # value is not exactly 64 KiB, but w/ 2 places, value is 64.00 KiB
        # so the trailing 0s are stripped.
        self.assertEqual(s.humanReadable(max_places=2), "64 KiB")
        # since all significant digits are shown, there are no trailing 0s.
        self.assertEqual(s.humanReadable(max_places=None), "63.9990234375 KiB")

        # deviation is less than 1/2 of 1% of 1024
        s = Size(16384 - (1024/100//2))
        self.assertEqual(s.humanReadable(max_places=2), "16 KiB")
        # deviation is greater than 1/2 of 1% of 1024
        s = Size(16384 - ((1024/100//2) + 1))
        self.assertEqual(s.humanReadable(max_places=2), "15.99 KiB")

        s = Size(0x10000000000000)
        self.assertEqual(s.humanReadable(max_places=2), "4 PiB")
開發者ID:dashea,項目名稱:blivet,代碼行數:21,代碼來源:size_test.py

示例14: testRoundToNearest

    def testRoundToNearest(self):
        self.assertEqual(size.ROUND_DEFAULT, size.ROUND_HALF_UP)

        s = Size("10.3 GiB")
        self.assertEqual(s.roundToNearest(GiB), Size("10 GiB"))
        self.assertEqual(s.roundToNearest(GiB, rounding=size.ROUND_DEFAULT),
                         Size("10 GiB"))
        self.assertEqual(s.roundToNearest(GiB, rounding=size.ROUND_DOWN),
                         Size("10 GiB"))
        self.assertEqual(s.roundToNearest(GiB, rounding=size.ROUND_UP),
                         Size("11 GiB"))
        # >>> Size("10.3 GiB").convertTo(MiB)
        # Decimal('10547.19999980926513671875')
        self.assertEqual(s.roundToNearest(MiB), Size("10547 MiB"))
        self.assertEqual(s.roundToNearest(MiB, rounding=size.ROUND_UP),
                         Size("10548 MiB"))
        self.assertIsInstance(s.roundToNearest(MiB), Size)
        with self.assertRaises(ValueError):
            s.roundToNearest(MiB, rounding='abc')

        # arbitrary decimal rounding constants are not allowed
        from decimal import ROUND_HALF_DOWN
        with self.assertRaises(ValueError):
            s.roundToNearest(MiB, rounding=ROUND_HALF_DOWN)

        s = Size("10.51 GiB")
        self.assertEqual(s.roundToNearest(GiB), Size("11 GiB"))
        self.assertEqual(s.roundToNearest(GiB, rounding=size.ROUND_DEFAULT),
                         Size("11 GiB"))
        self.assertEqual(s.roundToNearest(GiB, rounding=size.ROUND_DOWN),
                         Size("10 GiB"))
        self.assertEqual(s.roundToNearest(GiB, rounding=size.ROUND_UP),
                         Size("11 GiB"))

        s = Size("513 GiB")
        self.assertEqual(s.roundToNearest(GiB), s)
        self.assertEqual(s.roundToNearest(TiB), Size("1 TiB"))
        self.assertEqual(s.roundToNearest(TiB, rounding=size.ROUND_DOWN),
                         Size(0))

        # test Size parameters
        self.assertEqual(s.roundToNearest(Size("128 GiB")), Size("512 GiB"))
        self.assertEqual(s.roundToNearest(Size("1 KiB")), Size("513 GiB"))
        self.assertEqual(s.roundToNearest(Size("1 TiB")), Size("1 TiB"))
        self.assertEqual(s.roundToNearest(Size("1 TiB"), rounding=size.ROUND_DOWN), Size(0))
        self.assertEqual(s.roundToNearest(Size(0)), Size(0))
        self.assertEqual(s.roundToNearest(Size("13 GiB")), Size("507 GiB"))

        with self.assertRaises(ValueError):
            s.roundToNearest(Size("-1 B"))
開發者ID:dashea,項目名稱:blivet,代碼行數:50,代碼來源:size_test.py

示例15: testNegative

 def testNegative(self):
     s = Size("-500MiB")
     self.assertEqual(s.humanReadable(), "-500 MiB")
     self.assertEqual(s.convertTo(B), -524288000)
開發者ID:dashea,項目名稱:blivet,代碼行數:4,代碼來源:size_test.py


注:本文中的blivet.size.Size類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。