def test_repeat_lambda(self): out = list(python.repeat_lambda(dict, 2)) self.assertEqual(len(out), 2) self.assertIsNot(out[0], out[1]) out = list(python.repeat_lambda(dict, -10)) self.assertEqual(len(out), 0)
def compute_objs_diff(self, data, objs, instance): """ Find out the right number of related objects to be saved, given the data and the objects that are already present. :param data: list of data for sub forms :param objs: objs that are already there :param instance: a model instance """ objs_to_delete = [] diff = len(data) - len(objs) # manage obj addition if diff > 0: kwargs = {self.origin_model_name: instance} objs = chain( objs, repeat_lambda(lambda: self.foreign_model(**kwargs), diff) ) # manage obj deletion elif diff < 0: objs, objs_to_delete = isplit(objs, len(data)) return objs, objs_to_delete