def test_any_range(self): ''' test that the any_range method works correctly ''' actual = list(any_range(Fraction(0), Fraction(4, 3), Fraction(1, 3))) expect = [ Fraction(0, 1), Fraction(1, 3), Fraction(2, 3), Fraction(3, 3) ] self.assertEqual(actual, expect)
def from_function(klass, function, interval=0.01): ''' A factory method to create a preference collection from a value function. :param function: The function to generate intervals with :param interval: The interval step size to use :returns: An initialized Preference ''' points = [] for x in any_range(0, 1, interval): points.append((x, function(x))) points.append((1, function(1))) return klass(None, points)
def as_collection(self): ''' Return the underlying resource as a collection of resources (one for each discrete item :returns: The collection of resources ''' (start, span) = self.value step = F(start + span, self.resolution) pieces = [] points = any_range(start + step, start + span, step) for point in points: start, piece = point, ContinuousResource(start, point - start) pieces.append(piece) return pieces
def as_collection(self): ''' Return the underlying resource as a collection of resources (one for each discrete item :returns: The collection of resources ''' pieces = [] value = self.actual_value() steps = value / self.resolution for start, stop in self.value: step = steps * ((stop - start) / value) for point in any_range(start + step, stop, step): pieces.append(IntervalResource((start, point))) start = point return pieces