A community-maintained Python framework for creating mathematical animations.
Changed return type of batch_by_property
Grant Sandersond5aeeb0e8c1d
diff --git a/camera/camera.py b/camera/camera.pyindex 0b284e85b..546ba94b5 100644--- a/camera/camera.py+++ b/camera/camera.py@@ -193,13 +193,13 @@ class Camera(object):raise Exception("Trying to display something which is not of type Mobject")- batches = batch_by_property(mobjects, get_mobject_type)+ batch_type_pairs = batch_by_property(mobjects, get_mobject_type)#Display in these batches- for batch in batches:+ for batch, batch_type in batch_type_pairs:#check what the type is, and call the appropriate functionfor mobject_type, func in type_func_pairs:- if isinstance(batch[0], mobject_type):+ if batch_type == mobject_type:func(batch)## Methods associated with svg rendering@@ -216,12 +216,12 @@ class Camera(object):def display_multiple_vectorized_mobjects(self, vmobjects):if len(vmobjects) == 0:return- batches = batch_by_property(+ batch_file_pairs = batch_by_property(vmobjects,lambda vm : vm.get_background_image_file())- for batch in batches:- if batch[0].get_background_image_file():+ for batch, file_name in batch_file_pairs:+ if file_name:self.display_multiple_background_colored_vmobject(batch)else:self.display_multiple_non_background_colored_vmobjects(batch)@@ -550,8 +550,7 @@ class BackgroundColoredVMobjectDisplayer(object):mode = "RGBA" if pixel_array.shape[2] == 4 else "RGB"return self.resize_background_array(background_array, width, height, mode)- def get_background_array(self, cvmobject):- file_name = cvmobject.get_background_image_file()+ def get_background_array(self, file_name):if file_name in self.file_name_to_pixel_array_map:return self.file_name_to_pixel_array_map[file_name]full_path = get_full_raster_image_path(file_name)@@ -566,12 +565,12 @@ class BackgroundColoredVMobjectDisplayer(object):return arraydef display(self, *cvmobjects):- batches = batch_by_property(+ batch_image_file_pairs = batch_by_property(cvmobjects, lambda cv : cv.get_background_image_file())curr_array = None- for batch in batches:- background_array = self.get_background_array(batch[0])+ for batch, image_file in batch_image_file_pairs:+ background_array = self.get_background_array(image_file)for cvmobject in batch:self.camera.display_vectorized(cvmobject, self.canvas)self.canvas.flush()diff --git a/helpers.py b/helpers.pyindex 80ee9e331..848f0adca 100644--- a/helpers.py+++ b/helpers.py@@ -227,22 +227,30 @@ def adjacent_pairs(objects):return zip(objects, list(objects[1:])+[objects[0]])def batch_by_property(items, property_func):- batches = []- def add_batch(batch):+ """+ Takes in a list, and returns a list of tuples, (batch, prop)+ such that all items in a batch have the same output when+ put into property_func, and such that chaining all these+ batches together would give the original list.+ """+ batch_prop_pairs = []+ def add_batch_prop_pair(batch):if len(batch) > 0:- batches.append(batch)+ batch_prop_pairs.append(+ (batch, property_func(batch[0]))+ )curr_batch = []curr_prop = Nonefor item in items:prop = property_func(item)if prop != curr_prop:- add_batch(curr_batch)+ add_batch_prop_pair(curr_batch)curr_prop = propcurr_batch = [item]else:curr_batch.append(item)- add_batch(curr_batch)- return batches+ add_batch_prop_pair(curr_batch)+ return batch_prop_pairsdef complex_to_R3(complex_num):return np.array((complex_num.real, complex_num.imag, 0))