def t_ID(self, t): r'[a-zA-Z]+' if t.value in self._RESERVED.keys(): t.type = self._RESERVED[t.value] return t if Information.is_valid_symbol(t.value) or \ Information.is_valid_category(t.value): t.type = self._INFORMATION_UNIT return t if Duration.is_valid_symbol(t.value): t.type = self._DURATION_UNIT return t raise LexingError('Unrecognised token or unit \'{0.value}\' at ' 'position {0.lexpos}'.format(t))
def test_determine_unit_symbol_quantity(self): for category in [ Information.BINARY_BITS, Information.BINARY_BYTES, Information.DECIMAL_BITS, Information.DECIMAL_BYTES ]: expanded = Information._expand_units(category) for unit, bits in six.iteritems(expanded): # ensure this unit is used when we want to represent the exact # amount of data that it is equivalent to self.assertEqual( Information._determine_unit_symbol_quantity( bits, category), unit) # we should also use the same unit for this number of bits + 1 # (assuming units are not close together) self.assertEqual( Information._determine_unit_symbol_quantity( bits + 1, category), unit)
def from_quantity_units(cls, quantity, information_unit, duration_unit): """ Initialise a new speed object from a quantity and unit string. :param quantity: The number of the unit. :param information_unit: The information part of the unit, e.g. 'GiB'. :param duration_unit: The duration part of the unit, e.g. 'week'. :return: A `Speed` object representing the quantity and unit. """ information = Information.from_quantity_unit(quantity, information_unit) duration = Duration.from_quantity_unit(1, duration_unit) return Speed(information, duration)
def test_eq_false(self): self.assertFalse( Information(22, Information.MEBIBYTES) == Information( 2528, Information.KIBIBYTES))
def test_sub(self): self.assertEqual( Speed.TEN_GIGABIT - Speed.GIGABIT, Speed(Information(90, Information.GIGABITS), Duration(seconds=10)))
class TestSpeed(unittest.TestCase): _INFORMATION = Information(10) _SPEED = Speed(_INFORMATION) def test_init_instant(self): with self.assertRaises(ValueError): Speed(Information(1), Duration.ZERO) def test_from_quantity_units(self): self.assertEqual( Speed.from_quantity_units(1.35, 'kB', 'weeks'), Speed(Information(1.35, Information.KILOBYTES), Duration(weeks=1))) def test_per_second(self): self.assertEqual(Speed.FORTY_GIGABIT._per_second, Information(40000, Information.MEGABITS)) def test_for_duration(self): self.assertEqual(self._SPEED.for_duration(Duration(minutes=1)), self._INFORMATION * 60) def test_lt_bad_class(self): with self.assertRaises(TypeError): _ = Speed.TEN_GIGABIT < 1 def test_lt_false(self): self.assertFalse(Speed.TEN_GIGABIT < Speed.GIGABIT) def test_lt_true(self): self.assertLess(Speed.GIGABIT, Speed.TEN_GIGABIT) def test_le_bad_class(self): with self.assertRaises(TypeError): _ = Speed.GIGABIT <= 1 def test_le_false(self): self.assertFalse(Speed.TEN_GIGABIT <= Speed.GIGABIT) def test_le_true_less(self): self.assertLessEqual(Speed.GIGABIT, Speed.TEN_GIGABIT) def test_le_true_equal(self): self.assertLessEqual(Speed.TEN_GIGABIT, Speed.TEN_GIGABIT) def test_eq_bad_class(self): with self.assertRaises(TypeError): _ = Speed.GIGABIT == 1 def test_eq_false(self): self.assertFalse(Speed.GIGABIT == Speed.TEN_GIGABIT) def test_eq_true(self): self.assertEqual( Speed.GIGABIT, Speed(Information(10, Information.GIGABITS), Duration(seconds=10))) def test_ne_bad_class(self): with self.assertRaises(TypeError): _ = Speed.GIGABIT != 1 def test_ne_false(self): self.assertFalse(Speed.FORTY_GIGABIT != Speed( Information(80, Information.GIGABITS), Duration(seconds=2))) def test_ne_true(self): self.assertNotEqual(Speed.FORTY_GIGABIT, Speed.HUNDRED_GIGABIT) def test_ge_bad_class(self): with self.assertRaises(TypeError): _ = Speed.GIGABIT >= 1 def test_ge_false(self): self.assertFalse(Speed.GIGABIT >= Speed.TEN_GIGABIT) def test_ge_true_less(self): self.assertGreaterEqual(Speed.TEN_GIGABIT, Speed.GIGABIT) def test_ge_true_equal(self): self.assertGreaterEqual(Speed.TEN_GIGABIT, Speed.TEN_GIGABIT) def test_gt_bad_class(self): with self.assertRaises(TypeError): _ = Speed.GIGABIT > 1 def test_gt_false(self): self.assertFalse(Speed.GIGABIT > Speed.TEN_GIGABIT) def test_gt_true(self): self.assertGreater(Speed.TEN_GIGABIT, Speed.GIGABIT) def test_add_bad_class(self): with self.assertRaises(TypeError): _ = Speed.GIGABIT + 1 def test_add(self): self.assertEqual( Speed(Information(500, Information.MEGABITS)) + Speed(Information(2.5, Information.GIGABITS), Duration(seconds=5)), Speed.GIGABIT) def test_sub_bad_class(self): with self.assertRaises(TypeError): _ = Speed.GIGABIT - 1 def test_sub_zero(self): with self.assertRaises(ArithmeticError): _ = Speed.GIGABIT * 10 - Speed.TEN_GIGABIT def test_sub_negative(self): with self.assertRaises(ArithmeticError): _ = Speed.GIGABIT - Speed.TEN_GIGABIT def test_sub(self): self.assertEqual( Speed.TEN_GIGABIT - Speed.GIGABIT, Speed(Information(90, Information.GIGABITS), Duration(seconds=10))) def test_mul_bad_class(self): with self.assertRaises(TypeError): _ = Speed.GIGABIT * '' def test_mul(self): self.assertEqual(Speed.TEN_GIGABIT * 4, Speed.FORTY_GIGABIT) def test_truediv_bad_class(self): with self.assertRaises(TypeError): _ = Speed.GIGABIT / '' def test_truediv_zero(self): with self.assertRaises(ZeroDivisionError): _ = Speed.GIGABIT / 0 def test_truediv_low(self): # 1.33 should go down self.assertEqual( Speed(Information(4), Duration(1)) / 3, Speed(Information(1), Duration(1))) def test_truediv_high(self): # 1.66 should go up self.assertEqual( Speed(Information(10), Duration(1)) / 6, Speed(Information(20), Duration(10))) def test_floordiv_bad_class(self): with self.assertRaises(TypeError): _ = Speed.GIGABIT // '' def test_floordiv_zero(self): with self.assertRaises(ZeroDivisionError): _ = Speed.GIGABIT // 0 def test_floordiv_low(self): # 1.33 should go down self.assertEqual( Speed(Information(4), Duration(1)) // 3, Speed(Information(1), Duration(1))) def test_floordiv_high(self): # 1.66 should go down self.assertEqual( Speed(Information(10), Duration(1)) // 6, Speed(Information(1), Duration(1))) def test_bool_true(self): self.assertTrue(Speed.HUNDRED_MEGABIT) def test_bool_false(self): self.assertFalse(Speed.ZERO) def test_format_default(self): self.assertEqual('{0}'.format(Speed.GIGABIT), '119.21 MiB/s') def test_format_zero(self): self.assertEqual('{0}'.format(Speed.ZERO), '0 B/s') def test_format_info_unit(self): # force Gb self.assertEqual('{0:Gb}'.format(Speed.GIGABIT), '1Gb/s') def test_format_invalid_time_unit(self): with self.assertRaises(TypeError): '{0:/z}'.format(self._SPEED) def test_format_invalid_info_unit(self): with self.assertRaises(TypeError): '{0:foo}'.format(Duration.SECOND) def test_format_separator_info_unit(self): speed = Speed(Information(1, Information.TERABITS)) self.assertEqual('{0: Gb}'.format(speed), '1,000 Gb/s') def test_format_time_unit_singular(self): # use per minute instead of seconds, and work out the quantity in binary # bytes self.assertEqual('{0:/m}'.format(Speed.GIGABIT), '6.98GiB/m') def test_format_time_unit_integral(self): self.assertEqual('{0:/5m}'.format(Speed.TEN_GIGABIT), '349.25GiB/5m') def test_format_time_unit_fractional(self): self.assertEqual('{0:/2.3d}'.format(Speed.TEN_MEGABIT), '231.34GiB/2.3d') def test_format_separator_time_unit(self): self.assertEqual('{0: /m}'.format(Speed.GIGABIT), '6.98 GiB/m') def test_format_info_category_time_unit(self): # work out the quantity in binary bit units per hour, putting a space # between the quantity and unit, and using default formatting for the # quantity, self.assertEqual('{0: bb/h}'.format(Speed.GIGABIT), '3.27 Tib/h') def test_format_info_unit_time_unit(self): # use per minute instead of seconds, and work out the quantity in MiB self.assertEqual('{0: MiB/m}'.format(Speed.HUNDRED_MEGABIT), '715.26 MiB/m') def test_format(self): # show the quantity of information processed per month to 2dp with comma # separated thousands, with a space after, then a decimal bytes unit self.assertEqual('{0:,.2f|dB/mo}'.format(Speed.GIGABIT), '328.50TB/mo') def test_repr(self): self.assertEqual(repr(self._SPEED), '<Speed(<Information(10)>, <Duration(1000000000)>)>') def test_str(self): self.assertEqual(str(self._SPEED), '1.25 B/s')
except ValueError as e: raise TypeError(e) information = self.information * nanos / self.duration.nanoseconds time_fmt = unit if quantity == 1 else '{0}{1}'.format(quantity, unit) return '{0:{1}}/{2}'.format(information, lhs, time_fmt) def __str__(self): return '{0}'.format(self) Speed.ZERO = Speed(Information.ZERO) # Ethernet Speed.TEN_MEGABIT = Speed(Information(10, Information.MEGABITS)) Speed.HUNDRED_MEGABIT = Speed.TEN_MEGABIT * 10 Speed.GIGABIT = Speed.HUNDRED_MEGABIT * 10 Speed.TEN_GIGABIT = Speed.GIGABIT * 10 Speed.FORTY_GIGABIT = Speed.TEN_GIGABIT * 4 Speed.HUNDRED_GIGABIT = Speed.TEN_GIGABIT * 10 # E-carrier Speed.E0 = Speed(Information(64, Information.KILOBITS)) Speed.E1 = Speed(Information(2.048, Information.MEGABITS)) Speed.E2 = Speed(Information(8.448, Information.MEGABITS)) Speed.E3 = Speed(Information(34.368, Information.MEGABITS)) Speed.E4 = Speed(Information(139.264, Information.MEGABITS)) Speed.E5 = Speed(Information(565.148, Information.MEGABITS)) # T-carrier signaling
def test_format_separator_info_unit(self): speed = Speed(Information(1, Information.TERABITS)) self.assertEqual('{0: Gb}'.format(speed), '1,000 Gb/s')
def test_eq_true(self): self.assertEqual( Speed.GIGABIT, Speed(Information(10, Information.GIGABITS), Duration(seconds=10)))
def test_duration_information_speed(self): duration = Information(17.3, Information.GIGABYTES).at_speed( Speed(Information(688.3, Information.KILOBYTES))) self.assertEqual(self.parser.parse('17.3GB at 688.3kB/s'), duration)
def test_from_quantity_units(self): self.assertEqual( Speed.from_quantity_units(1.35, 'kB', 'weeks'), Speed(Information(1.35, Information.KILOBYTES), Duration(weeks=1)))
def test_information_constructor(self): self.assertEqual(self.parser.parse('10.4Gb'), Information(10.4, Information.GIGABITS))
def test_information_speed_duration(self): self.assertEqual(self.parser.parse('10Gb/s for 11 minutes'), Information(10 * 60 * 11, Information.GIGABITS))
def test_expression_speed(self): self.assertEqual( self.parser.parse('11.25Yib/8years'), Speed(Information(13600415470664578215444480), Duration(years=8)))
def test_expression_information(self): self.assertEqual(self.parser.parse('10Gb'), Information(10, Information.GIGABITS))
def test_eq_true(self): self.assertEqual(Information(22, Information.MEBIBYTES), Information(22528, Information.KIBIBYTES))
def test_truediv_high(self): # 1.66 should go up self.assertEqual( Speed(Information(10), Duration(1)) / 6, Speed(Information(20), Duration(10)))
def test_speed_constructor(self): self.assertEqual(self.parser.parse('12 MiB/3s'), Speed(Information(4, Information.MEBIBYTES)))
def test_floordiv_low(self): # 1.33 should go down self.assertEqual( Speed(Information(4), Duration(1)) // 3, Speed(Information(1), Duration(1)))
def test_speed_information_duration(self): self.assertEqual(self.parser.parse('11 b in 2 nanoseconds'), Speed(Information(11), Duration(nanoseconds=2)))
def test_floordiv_high(self): # 1.66 should go down self.assertEqual( Speed(Information(10), Duration(1)) // 6, Speed(Information(1), Duration(1)))
def p_speed_constructor(self, p): 'speed : NUMBER speed_unit' logger.debug('speed = number %s, speed unit %s', p[1], p[2]) information_unit, duration = p[2] information = Information.from_quantity_unit(p[1], information_unit) p[0] = information.in_duration(duration)
def test_per_second(self): self.assertEqual(Speed.FORTY_GIGABIT._per_second, Information(40000, Information.MEGABITS))
def p_information_constructor(self, p): 'information : NUMBER INFORMATION_UNIT' logger.debug('information = number %s, information unit %s', p[1], p[2]) p[0] = Information.from_quantity_unit(p[1], p[2])
def test_ne_false(self): self.assertFalse(Speed.FORTY_GIGABIT != Speed( Information(80, Information.GIGABITS), Duration(seconds=2)))
def test_le_false(self): self.assertFalse(Information(10) <= Information(1))
def test_le_true_equal(self): self.assertLessEqual(Information(10), Information(10))
def test_init_instant(self): with self.assertRaises(ValueError): Speed(Information(1), Duration.ZERO)
def test_add(self): self.assertEqual( Speed(Information(500, Information.MEGABITS)) + Speed(Information(2.5, Information.GIGABITS), Duration(seconds=5)), Speed.GIGABIT)
def test_eq_bad_class(self): with self.assertRaises(TypeError): _ = Information(1) == 1