def test_is_predicate(self): class dummyPred: def __call__(self, item): pass def __str__(self): return "dummyPred" pred = dummyPred() assert predicates.is_predicate( pred), "a class with call and str methods satisfies is_predicate()" pred = object() assert not predicates.is_predicate( pred ), "a class without call and str doesn't satisfy is_predicate()" pred = predicates.predicate() assert predicates.is_predicate( pred), "an instance of predicate satisfies is_predicate()" pred = predicates.equal_to(1) assert predicates.is_predicate( pred ), "an instance of a subclass of predicate satisfies is_predicate()"
def __get_index(self, start): """ finds the index that start specifies Args: start: an indicator of a position in an order can be a predicate or an index in the ordering Returns: the index in the given list that start refers to """ if predicates.is_predicate(start): index = 0 while index < self.size() and not start(self.objects[index]): index += 1 return index else: return start
def __init__(self, filter_pred=None): """ Constructor used to set-up history. If the history is given a filter, it will ignore (drop) objects that don't satisfy the filter predicate. Args: filter_pred: an optional predicate to filter incoming data_objects """ self.objects = [] self.filter = predicates.always_true() if filter_pred is not None: if predicates.is_predicate(filter_pred): self.filter = filter_pred else: raise TypeError( "The given filter was an instance of predicate.") self.retrieved_cursor = 0
def __get_index(self, start, ordered): """ finds the index that start specifies Args: start: an indicator of a position in an order can be a predicate, a TimeType time stamp or an index in the ordering ordered: the list to clear Returns: the index in the given list that start refers to """ if predicates.is_predicate(start): index = 0 while index < len(ordered) and not start(ordered[index]): index += 1 return index elif isinstance(start, TimeType): index = 0 while index < len(ordered) and ordered[index].get_time() < start: index += 1 return index else: return start