def __init__(self, iterable, order, func): '''Create an OrderedIterable. Args: iterable: The iterable sequence to be ordered. order: +1 for ascending, -1 for descending. func: The function to select the sorting key. ''' if not is_iterable(iterable): raise TypeError("Cannot construct OrderedQuery from non-iterable {0}".format(str(type(iterable))[7: -2])) Util.assert_type(order, int) if not is_callable(func): raise TypeError('func is not callable') assert abs(order) == 1, 'order argument must be +1 or -1' super(OrderedQuery, self).__init__(iterable) self.funcs = [(order, func)]
def __init__(self, iterable, order, func): '''Create an OrderedIterable. Args: iterable: The iterable sequence to be ordered. order: +1 for ascending, -1 for descending. func: The function to select the sorting key. ''' if not is_iterable(iterable): raise TypeError( "Cannot construct OrderedQuery from non-iterable {0}".format( str(type(iterable))[7:-2])) Util.assert_type(order, int) if not is_callable(func): raise TypeError('func is not callable') assert abs(order) == 1, 'order argument must be +1 or -1' super(OrderedQuery, self).__init__(iterable) self.funcs = [(order, func)]
def take(self, n): Util.assert_type(n, (int, long)) n = max(0, n) return Query(self, func=lambda: itertools.islice(self, n))