本文整理汇总了Python中bytesize.SizeStruct.new_from_str方法的典型用法代码示例。如果您正苦于以下问题:Python SizeStruct.new_from_str方法的具体用法?Python SizeStruct.new_from_str怎么用?Python SizeStruct.new_from_str使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bytesize.SizeStruct
的用法示例。
在下文中一共展示了SizeStruct.new_from_str方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testMod
# 需要导入模块: from bytesize import SizeStruct [as 别名]
# 或者: from bytesize.SizeStruct import new_from_str [as 别名]
def testMod(self):
x = SizeStruct.new_from_str("1024 B")
y = SizeStruct.new_from_str("1000 B")
actual = x.mod(y).get_bytes()
expected = (24, 1)
self.assertEqual(actual, expected)
# when modding the signs are ignored
x = SizeStruct.new_from_str("1024 B")
y = SizeStruct.new_from_str("-1000 B")
actual = x.mod(y).get_bytes()
expected = (24, 1)
self.assertEqual(actual, expected)
x = SizeStruct.new_from_str("-1024 B")
y = SizeStruct.new_from_str("1000 B")
actual = x.mod(y).get_bytes()
expected = (24, 1)
self.assertEqual(actual, expected)
x = SizeStruct.new_from_str("-1024 B")
y = SizeStruct.new_from_str("-1000 B")
actual = x.mod(y).get_bytes()
expected = (24, 1)
self.assertEqual(actual, expected)
x = SizeStruct.new_from_str("1024 B")
y = SizeStruct.new_from_str("1024 B")
actual = x.mod(y).get_bytes()
expected = (0, 0)
self.assertEqual(actual, expected)
示例2: testCmpBytes
# 需要导入模块: from bytesize import SizeStruct [as 别名]
# 或者: from bytesize.SizeStruct import new_from_str [as 别名]
def testCmpBytes(self):
x = SizeStruct.new_from_str("1 KiB")
# result > 0
y = 1023
cmpResult = SizeStruct.cmp_bytes(x, y, False)
self.assertGreater(cmpResult, 0)
# result < 0
y = 1025
cmpResult = SizeStruct.cmp_bytes(x, y, False)
self.assertLess(cmpResult, 0)
# result == 0
y = 1024
cmpResult = SizeStruct.cmp_bytes(x, y, False)
self.assertEqual(cmpResult, 0)
# test with abs == True
x = SizeStruct.new_from_str("-1 KiB")
# result > 0
y = 1023
cmpResult = SizeStruct.cmp_bytes(x, y, True)
self.assertGreater(cmpResult, 0)
# result < 0
y = 1025
cmpResult = SizeStruct.cmp_bytes(x, y, True)
self.assertLess(cmpResult, 0)
# result == 0
y = 1024
cmpResult = SizeStruct.cmp_bytes(x, y, True)
self.assertEqual(cmpResult, 0)
示例3: testSgn
# 需要导入模块: from bytesize import SizeStruct [as 别名]
# 或者: from bytesize.SizeStruct import new_from_str [as 别名]
def testSgn(self):
sgn = SizeStruct.new_from_str("12 KiB").sgn()
self.assertEqual(sgn, 1)
sgn = SizeStruct.new_from_str("0 MB").sgn()
self.assertEqual(sgn, 0)
sgn = SizeStruct.new_from_str("-12 GiB").sgn()
self.assertEqual(sgn, -1)
示例4: testTrueDiv
# 需要导入模块: from bytesize import SizeStruct [as 别名]
# 或者: from bytesize.SizeStruct import new_from_str [as 别名]
def testTrueDiv(self):
x = SizeStruct.new_from_str("1024 B")
y = SizeStruct.new_from_str("-102.4 B") # rounds to whole bytes
divResult = float(x.true_div(y)[:15].replace(locale.nl_langinfo(locale.RADIXCHAR), ".")) # just some number to cover accurancy and not cross max float range
self.assertAlmostEqual(divResult, 1024.0/-102.0)
x = SizeStruct.new_from_str("1 MiB")
y = SizeStruct.new_from_str("1 KiB")
divResult = float(x.true_div(y)[:15].replace(locale.nl_langinfo(locale.RADIXCHAR), ".")) # just some number to cover accurancy and not cross max float range
self.assertAlmostEqual(divResult, 1024.0)
示例5: testDivInt
# 需要导入模块: from bytesize import SizeStruct [as 别名]
# 或者: from bytesize.SizeStruct import new_from_str [as 别名]
def testDivInt(self):
x = SizeStruct.new_from_str("1 MiB")
y = 1024
divResult = x.div_int(y).get_bytes()
self.assertEqual(divResult, (1024, 1))
x = SizeStruct.new_from_str("-1 MiB")
y = 1077
divResult = x.div_int(y).get_bytes()
self.assertEqual(divResult, (973, -1))
示例6: testMulFloatStr
# 需要导入模块: from bytesize import SizeStruct [as 别名]
# 或者: from bytesize.SizeStruct import new_from_str [as 别名]
def testMulFloatStr(self):
x = SizeStruct.new_from_str("8 B")
actual = x.mul_float_str("1.51").get_bytes()
self.assertEqual(actual, (12, 1))
x = SizeStruct.new_from_str("-8 B")
actual = x.mul_float_str("1.51").get_bytes()
self.assertEqual(actual, (12, -1))
x = SizeStruct.new_from_str("8 B")
actual = x.mul_float_str("-1.51").get_bytes()
self.assertEqual(actual, (12, -1))
示例7: testShrinkDivInt
# 需要导入模块: from bytesize import SizeStruct [as 别名]
# 或者: from bytesize.SizeStruct import new_from_str [as 别名]
def testShrinkDivInt(self):
x = SizeStruct.new_from_str("100 B")
y = 11
x.shrink_div_int(y)
actual = x.get_bytes()
expected = (9, 1)
self.assertEqual(actual, expected)
x = SizeStruct.new_from_str("98 B")
y = 11
x.shrink_div_int(y)
actual = x.get_bytes()
expected = (8, 1)
self.assertEqual(actual, expected)
示例8: testTrueDivInt
# 需要导入模块: from bytesize import SizeStruct [as 别名]
# 或者: from bytesize.SizeStruct import new_from_str [as 别名]
def testTrueDivInt(self):
x = SizeStruct.new_from_str("1000 B")
y = 100
divResult = float(x.true_div_int(y)[:15]) # just some number to cover accuracy and not cross max float range
self.assertAlmostEqual(divResult, 1000.0/100.0)
x = SizeStruct.new_from_str("-1 MiB")
y = 1024
divResult = float(x.true_div_int(y)[:15]) # just some number to cover accuracy and not cross max float range
self.assertAlmostEqual(divResult, -1024.0)
x = SizeStruct.new_from_str("0 MiB")
y = 1024
divResult = float(x.true_div_int(y)[:15]) # just some number to cover accuracy and not cross max float range
self.assertAlmostEqual(divResult, 0.0)
示例9: testHumanReadable
# 需要导入模块: from bytesize import SizeStruct [as 别名]
# 或者: from bytesize.SizeStruct import new_from_str [as 别名]
def testHumanReadable(self):
strSizeStruct = SizeStruct.new_from_str("12 KiB").human_readable(KiB, 2, True)
self.assertEqual(strSizeStruct, "12 KiB")
strSizeStruct = SizeStruct.new_from_str("1 KB").human_readable(KiB, 2, False)
self.assertEqual(strSizeStruct, "0.98 KiB")
strSizeStruct = SizeStruct.new_from_str("100 GiB").human_readable(KiB, 2, False)
self.assertEqual(strSizeStruct, "100 GiB")
strSizeStruct = SizeStruct.new_from_str("100.00 GiB").human_readable(KiB, 2, False)
self.assertEqual(strSizeStruct, "100 GiB")
strSizeStruct = SizeStruct.new_from_str("100 GiB").human_readable(KiB, 0, False)
self.assertEqual(strSizeStruct, "100 GiB")
示例10: testCmp
# 需要导入模块: from bytesize import SizeStruct [as 别名]
# 或者: from bytesize.SizeStruct import new_from_str [as 别名]
def testCmp(self):
x = SizeStruct.new_from_str("1 KiB")
y = SizeStruct.new_from_str("-1 KiB")
# params: SizeStruct x, SizeStruct y, bool abs
# result > 0
cmpResult = SizeStruct.cmp(x, y, False)
self.assertGreater(cmpResult, 0)
# result < 0
cmpResult = SizeStruct.cmp(y, x, False)
self.assertLess(cmpResult, 0)
# result == 0
cmpResult = SizeStruct.cmp(y, x, True)
self.assertEqual(cmpResult, 0)
示例11: testShrinkBytes
# 需要导入模块: from bytesize import SizeStruct [as 别名]
# 或者: from bytesize.SizeStruct import new_from_str [as 别名]
def testShrinkBytes(self):
x = SizeStruct.new_from_str("8 B")
x.shrink_bytes(2)
actual = x.get_bytes()
expected = (6, 1)
self.assertEqual(actual, expected)
x = SizeStruct.new_from_str("8 B")
x.shrink_bytes(16)
actual = x.get_bytes()
expected = (8, -1)
self.assertEqual(actual, expected)
x = SizeStruct.new_from_str("-8 B")
x.shrink_bytes(8)
actual = x.get_bytes()
expected = (16, -1)
self.assertEqual(actual, expected)
示例12: testMulInt
# 需要导入模块: from bytesize import SizeStruct [as 别名]
# 或者: from bytesize.SizeStruct import new_from_str [as 别名]
def testMulInt(self):
x = SizeStruct.new_from_str("8 B")
y = 2
actual = x.mul_int(y).get_bytes()
expected = (16, 1)
self.assertEqual(actual, expected)
x = SizeStruct.new_from_str("0 B")
y = 1
actual = x.mul_int(y).get_bytes()
expected = (0, 0)
self.assertEqual(actual, expected)
x = SizeStruct.new_from_str("10 B")
y = 0
actual = x.mul_int(y).get_bytes()
expected = (0, 0)
self.assertEqual(actual, expected)
示例13: testGrowMulInt
# 需要导入模块: from bytesize import SizeStruct [as 别名]
# 或者: from bytesize.SizeStruct import new_from_str [as 别名]
def testGrowMulInt(self):
x = SizeStruct.new_from_str("8 B")
x.grow_mul_int(2)
actual = x.get_bytes()
expected = (16, 1)
self.assertEqual(actual, expected)
x = SizeStruct.new_from_str("0 B")
x.grow_mul_int(1)
actual = x.get_bytes()
expected = (0, 0)
self.assertEqual(actual, expected)
x = SizeStruct.new_from_str("10 B")
x.grow_mul_int(0)
actual = x.get_bytes()
expected = (0, 0)
self.assertEqual(actual, expected)
示例14: testDiv
# 需要导入模块: from bytesize import SizeStruct [as 别名]
# 或者: from bytesize.SizeStruct import new_from_str [as 别名]
def testDiv(self):
x = SizeStruct.new_from_str("1 KiB")
y = SizeStruct.new_from_str("-0.1 KiB")
divResult = x.div(y)
self.assertEqual(divResult, (10, -1))
x = SizeStruct.new_from_str("1 MiB")
y = SizeStruct.new_from_str("1 KiB")
divResult = x.div(y)
self.assertEqual(divResult, (1024, 1))
x = SizeStruct.new_from_str("1 GB")
y = SizeStruct.new_from_str("0.7 GB")
divResult = x.div(y)
self.assertEqual(divResult, (1, 1))
x = SizeStruct.new_from_str("-1 KiB")
y = SizeStruct.new_from_str("0.1 KiB")
divResult = x.div(y)
self.assertEqual(divResult, (10, -1))
示例15: testRoundToNearest
# 需要导入模块: from bytesize import SizeStruct [as 别名]
# 或者: from bytesize.SizeStruct import new_from_str [as 别名]
def testRoundToNearest(self):
x = SizeStruct.new_from_str("1500 B")
roundTo = SizeStruct.new_from_str("1 KiB")
actual = x.round_to_nearest(roundTo, ROUND_UP).get_bytes()
expected = (2048, 1)
self.assertEqual(actual, expected)
x = SizeStruct.new_from_str("1500 B")
roundTo = SizeStruct.new_from_str("1 KiB")
actual = x.round_to_nearest(roundTo, ROUND_DOWN).get_bytes()
expected = (1024, 1)
self.assertEqual(actual, expected)
x = SizeStruct.new_from_str("1500 B")
roundTo = SizeStruct.new_from_str("10 KiB")
actual = x.round_to_nearest(roundTo, ROUND_DOWN).get_bytes()
expected = (0, 0)
self.assertEqual(actual, expected)