Beispiel #1
0
    def at_speed(self, speed):
        """
        Find how long it would take to process this amount of data at a certain
        speed.

        :param speed: The speed of processing.
        :return: The time taken as a `datetime.timedelta`.
        """
        from nibble import Duration
        scale = self.bits / speed.information.bits
        return Duration(seconds=speed.duration.total_seconds() * scale)
Beispiel #2
0
    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)
Beispiel #3
0
    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))
Beispiel #4
0
    def __format__(self, format_spec):
        # Defaults to <the most appropriate binary bytes unit> per second
        # [number format|][ ][unit symbol or category][/[quantity][ ]time unit]

        lhs, _, time = format_spec.partition('/')

        if time:
            match = self.DURATION_REGEX.match(time)
            if match:
                # quantity provided
                quantity = float(match.group(1))
                if quantity.is_integer():
                    quantity = int(quantity)
                unit = match.group(2)
            else:
                # no quantity
                quantity = 1
                unit = time

            if not lhs:
                # this is a workaround to maintain the separator '/m' should
                # result in a separator not being printed, but '' is passed to
                # Information as the lhs, so it goes to default formatting
                lhs = 'bB'
        else:
            quantity = 1
            unit = 's'

        try:
            nanos = quantity * Duration.unit_nanoseconds(unit)
        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)
Beispiel #5
0
 def test_eq_true(self):
     self.assertEqual(Duration(seconds=1.5), Duration(milliseconds=1500))
Beispiel #6
0
 def test_init_weeks(self):
     self.assertEqual(Duration(weeks=1),
                      Duration(nanoseconds=10 ** 9 * 60 * 60 * 24 * 7))
Beispiel #7
0
 def test_ne_false(self):
     self.assertFalse(Speed.FORTY_GIGABIT != Speed(
         Information(80, Information.GIGABITS), Duration(seconds=2)))
Beispiel #8
0
 def test_for_duration(self):
     self.assertEqual(self._SPEED.for_duration(Duration(minutes=1)),
                      self._INFORMATION * 60)
Beispiel #9
0
 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)))
Beispiel #10
0
 def test_truediv_high(self):  # 1.66 should go up
     self.assertEqual(
         Speed(Information(10), Duration(1)) / 6,
         Speed(Information(20), Duration(10)))
Beispiel #11
0
 def test_from_timedelta(self):
     self.assertEqual(
         Duration.from_timedelta(datetime.timedelta(milliseconds=3500)),
         Duration(seconds=3.5))
Beispiel #12
0
 def test_total_seconds(self):
     self.assertEqual(Duration(seconds=1.5).total_seconds(),
                      datetime.timedelta(seconds=1.5).total_seconds())
Beispiel #13
0
 def test_is_valid_symbol_false(self):
     self.assertFalse(Duration.is_valid_symbol('dz'))
Beispiel #14
0
 def test_is_valid_symbol_true(self):
     self.assertTrue(Duration.is_valid_symbol('d'))
Beispiel #15
0
 def test_from_quantity_unit(self):
     self.assertEqual(Duration.from_quantity_unit(1.35, 'hours'),
                      Duration(hours=1.35))
Beispiel #16
0
 def test_init_years(self):
     self.assertEqual(Duration(years=1),
                      Duration(nanoseconds=10 ** 9 * 60 * 60 * 730 * 12))
Beispiel #17
0
 def test_init_months(self):
     self.assertEqual(Duration(months=1),
                      Duration(nanoseconds=10 ** 9 * 60 * 60 * 730))
Beispiel #18
0
 def test_add(self):
     self.assertEqual(
         Speed(Information(500, Information.MEGABITS)) +
         Speed(Information(2.5, Information.GIGABITS), Duration(seconds=5)),
         Speed.GIGABIT)
Beispiel #19
0
 def test_sub(self):
     self.assertEqual(
         Speed.TEN_GIGABIT - Speed.GIGABIT,
         Speed(Information(90, Information.GIGABITS), Duration(seconds=10)))
Beispiel #20
0
 def test_timedelta(self):
     self.assertEqual(Duration(minutes=91.5).timedelta,
                      datetime.timedelta(seconds=5490))
Beispiel #21
0
 def test_floordiv_low(self):  # 1.33 should go down
     self.assertEqual(
         Speed(Information(4), Duration(1)) // 3,
         Speed(Information(1), Duration(1)))
Beispiel #22
0
 def test_lt_true(self):
     self.assertLess(Duration(1), Duration(10))
Beispiel #23
0
 def test_floordiv_high(self):  # 1.66 should go down
     self.assertEqual(
         Speed(Information(10), Duration(1)) // 6,
         Speed(Information(1), Duration(1)))
Beispiel #24
0
 def test_le_false(self):
     self.assertFalse(Duration(10) <= Duration(1))
Beispiel #25
0
 def test_eq_true(self):
     self.assertEqual(
         Speed.GIGABIT,
         Speed(Information(10, Information.GIGABITS), Duration(seconds=10)))
Beispiel #26
0
 def test_le_true_equal(self):
     self.assertLessEqual(Duration(10), Duration(10))
Beispiel #27
0
 def test_str_y(self):
     self.assertEqual(str(Duration(months=24)), '2 years')
Beispiel #28
0
 def test_eq_false(self):
     self.assertFalse(Duration(seconds=2) == Duration(milliseconds=22))
Beispiel #29
0
 def test_eq_bad_class(self):
     with self.assertRaises(TypeError):
         _ = Duration(1) == 1
Beispiel #30
0
 def test_init_days(self):
     self.assertEqual(Duration(days=1),
                      Duration(nanoseconds=10 ** 9 * 60 * 60 * 24))