def __init__(self, data, date, delta): assert data, "data is required" if len(data) > 0: assert date, "date is required" assert delta, "delta is required" data = set(data) model_class = type(lists.first(data)) assert lists.all(lambda e: type(e) == model_class, data), "data contain items of different types" self.stored = model_class.objects.filter(pk__in=[e.id for e in data]) db = map(lambda e: dict(pk=e.id, date=date(e)), self.stored) non_cached_entities = set(filter(lambda e: e.pk not in [x["pk"] for x in db], data)) self.cached = data - non_cached_entities last_update_date = lambda e: filter(lambda x: x["pk"] == e.pk, db)[0]["date"] expired_places = filter(lambda e: timezone.now() - last_update_date(e) > delta, self.cached) self.for_insert = non_cached_entities self.for_update = expired_places
def test_first(self): self.assertEquals(lists.first([4, 2, 3]), 4) self.assertEquals(lists.first(set([4, 5, 3, 'a', 18])), 'a')