mirror of
https://github.com/3b1b/manim.git
synced 2025-08-21 05:44:04 +00:00
Replaced merge_config with merge_dicts_recursively
This commit is contained in:
parent
d88c301622
commit
e3ee258d91
5 changed files with 28 additions and 33 deletions
|
@ -9,7 +9,7 @@ from manimlib.mobject.number_line import NumberLine
|
||||||
from manimlib.mobject.svg.tex_mobject import TexMobject
|
from manimlib.mobject.svg.tex_mobject import TexMobject
|
||||||
from manimlib.mobject.types.vectorized_mobject import VGroup
|
from manimlib.mobject.types.vectorized_mobject import VGroup
|
||||||
from manimlib.utils.config_ops import digest_config
|
from manimlib.utils.config_ops import digest_config
|
||||||
from manimlib.utils.config_ops import merge_config
|
from manimlib.utils.config_ops import merge_dicts_recursively
|
||||||
from manimlib.utils.simple_functions import binary_search
|
from manimlib.utils.simple_functions import binary_search
|
||||||
from manimlib.utils.space_ops import angle_of_vector
|
from manimlib.utils.space_ops import angle_of_vector
|
||||||
|
|
||||||
|
@ -120,11 +120,11 @@ class Axes(VGroup, CoordinateSystem):
|
||||||
self.shift(self.center_point)
|
self.shift(self.center_point)
|
||||||
|
|
||||||
def create_axis(self, min_val, max_val, axis_config):
|
def create_axis(self, min_val, max_val, axis_config):
|
||||||
new_config = merge_config([
|
new_config = merge_dicts_recursively(
|
||||||
axis_config,
|
|
||||||
{"x_min": min_val, "x_max": max_val},
|
|
||||||
self.number_line_config,
|
self.number_line_config,
|
||||||
])
|
{"x_min": min_val, "x_max": max_val},
|
||||||
|
axis_config,
|
||||||
|
)
|
||||||
return NumberLine(**new_config)
|
return NumberLine(**new_config)
|
||||||
|
|
||||||
def coords_to_point(self, *coords):
|
def coords_to_point(self, *coords):
|
||||||
|
@ -398,10 +398,10 @@ class ComplexPlane(NumberPlane):
|
||||||
if abs(z.imag) > abs(z.real):
|
if abs(z.imag) > abs(z.real):
|
||||||
axis = self.get_y_axis()
|
axis = self.get_y_axis()
|
||||||
value = z.imag
|
value = z.imag
|
||||||
kwargs = merge_config([
|
kwargs = merge_dicts_recursively(
|
||||||
{"number_config": {"unit": "i"}},
|
|
||||||
kwargs,
|
kwargs,
|
||||||
])
|
{"number_config": {"unit": "i"}},
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
axis = self.get_x_axis()
|
axis = self.get_x_axis()
|
||||||
value = z.real
|
value = z.real
|
||||||
|
|
|
@ -7,7 +7,7 @@ from manimlib.mobject.numbers import DecimalNumber
|
||||||
from manimlib.mobject.types.vectorized_mobject import VGroup
|
from manimlib.mobject.types.vectorized_mobject import VGroup
|
||||||
from manimlib.utils.bezier import interpolate
|
from manimlib.utils.bezier import interpolate
|
||||||
from manimlib.utils.config_ops import digest_config
|
from manimlib.utils.config_ops import digest_config
|
||||||
from manimlib.utils.config_ops import merge_config
|
from manimlib.utils.config_ops import merge_dicts_recursively
|
||||||
from manimlib.utils.simple_functions import fdiv
|
from manimlib.utils.simple_functions import fdiv
|
||||||
from manimlib.utils.space_ops import normalize
|
from manimlib.utils.space_ops import normalize
|
||||||
|
|
||||||
|
@ -134,10 +134,10 @@ class NumberLine(Line):
|
||||||
scale_val=None,
|
scale_val=None,
|
||||||
direction=None,
|
direction=None,
|
||||||
buff=None):
|
buff=None):
|
||||||
number_config = merge_config([
|
number_config = merge_dicts_recursively(
|
||||||
|
self.decimal_number_config,
|
||||||
number_config or {},
|
number_config or {},
|
||||||
self.decimal_number_config
|
)
|
||||||
])
|
|
||||||
scale_val = scale_val or self.number_scale_val
|
scale_val = scale_val or self.number_scale_val
|
||||||
direction = direction or self.label_direction
|
direction = direction or self.label_direction
|
||||||
buff = buff or self.line_to_number_buff
|
buff = buff or self.line_to_number_buff
|
||||||
|
|
|
@ -10,7 +10,7 @@ from manimlib.mobject.types.vectorized_mobject import VGroup
|
||||||
from manimlib.mobject.types.vectorized_mobject import VectorizedPoint
|
from manimlib.mobject.types.vectorized_mobject import VectorizedPoint
|
||||||
from manimlib.scene.scene import Scene
|
from manimlib.scene.scene import Scene
|
||||||
from manimlib.utils.config_ops import digest_config
|
from manimlib.utils.config_ops import digest_config
|
||||||
from manimlib.utils.config_ops import merge_config
|
from manimlib.utils.config_ops import merge_dicts_recursively
|
||||||
|
|
||||||
|
|
||||||
class ThreeDScene(Scene):
|
class ThreeDScene(Scene):
|
||||||
|
@ -150,7 +150,8 @@ class SpecialThreeDScene(ThreeDScene):
|
||||||
config = {}
|
config = {}
|
||||||
else:
|
else:
|
||||||
config = self.low_quality_config
|
config = self.low_quality_config
|
||||||
ThreeDScene.__init__(self, **merge_config([kwargs, config]))
|
config = merge_dicts_recursively(config, kwargs)
|
||||||
|
ThreeDScene.__init__(self, **config)
|
||||||
|
|
||||||
def get_axes(self):
|
def get_axes(self):
|
||||||
axes = ThreeDAxes(**self.three_d_axes_config)
|
axes = ThreeDAxes(**self.three_d_axes_config)
|
||||||
|
@ -174,7 +175,7 @@ class SpecialThreeDScene(ThreeDScene):
|
||||||
return axes
|
return axes
|
||||||
|
|
||||||
def get_sphere(self, **kwargs):
|
def get_sphere(self, **kwargs):
|
||||||
config = merge_config([kwargs, self.sphere_config])
|
config = merge_dicts_recursively(self.sphere_config, kwargs)
|
||||||
return Sphere(**config)
|
return Sphere(**config)
|
||||||
|
|
||||||
def get_default_camera_position(self):
|
def get_default_camera_position(self):
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
from functools import reduce
|
|
||||||
import inspect
|
import inspect
|
||||||
import operator as op
|
import itertools as it
|
||||||
|
|
||||||
|
|
||||||
def instantiate(obj):
|
def instantiate(obj):
|
||||||
|
@ -53,32 +52,27 @@ def digest_config(obj, kwargs, caller_locals={}):
|
||||||
caller_locals = filtered_locals(caller_locals)
|
caller_locals = filtered_locals(caller_locals)
|
||||||
all_dicts = [kwargs, caller_locals, obj.__dict__]
|
all_dicts = [kwargs, caller_locals, obj.__dict__]
|
||||||
all_dicts += static_configs
|
all_dicts += static_configs
|
||||||
obj.__dict__ = merge_config(all_dicts)
|
obj.__dict__ = merge_dicts_recursively(*reversed(all_dicts))
|
||||||
|
|
||||||
|
|
||||||
# TODO, priority here is backwards from dict.update.
|
def merge_dicts_recursively(*dicts):
|
||||||
# Should I change the convention?
|
|
||||||
def merge_config(all_dicts):
|
|
||||||
"""
|
"""
|
||||||
Creates a dict whose keyset is the union of all the
|
Creates a dict whose keyset is the union of all the
|
||||||
input dictionaries. The value for each key is based
|
input dictionaries. The value for each key is based
|
||||||
on the first dict in the list with that key.
|
on the first dict in the list with that key.
|
||||||
|
|
||||||
First dicts have higher priority
|
dicts later in the list have higher priority
|
||||||
|
|
||||||
When values are dictionaries, it is applied recursively
|
When values are dictionaries, it is applied recursively
|
||||||
"""
|
"""
|
||||||
all_config = reduce(op.add, [list(d.items()) for d in all_dicts])
|
result = dict()
|
||||||
config = dict()
|
all_items = it.chain(*[d.items() for d in dicts])
|
||||||
for c in all_config:
|
for key, value in all_items:
|
||||||
key, value = c
|
if key in result and isinstance(result[key], dict) and isinstance(value, dict):
|
||||||
if key not in config:
|
result[key] = merge_dicts_recursively(result[key], value)
|
||||||
config[key] = value
|
|
||||||
else:
|
else:
|
||||||
# When two dictionaries have the same key, they are merged.
|
result[key] = value
|
||||||
if isinstance(value, dict) and isinstance(config[key], dict):
|
return result
|
||||||
config[key] = merge_config([config[key], value])
|
|
||||||
return config
|
|
||||||
|
|
||||||
|
|
||||||
def soft_dict_update(d1, d2):
|
def soft_dict_update(d1, d2):
|
||||||
|
|
|
@ -54,7 +54,7 @@ class SphereCylinderScene(SpecialThreeDScene):
|
||||||
}
|
}
|
||||||
|
|
||||||
def get_cylinder(self, **kwargs):
|
def get_cylinder(self, **kwargs):
|
||||||
config = merge_config([kwargs, self.sphere_config])
|
config = merge_dicts_recursively(self.sphere_config, kwargs)
|
||||||
return Cylinder(**config)
|
return Cylinder(**config)
|
||||||
|
|
||||||
def get_cylinder_caps(self):
|
def get_cylinder_caps(self):
|
||||||
|
|
Loading…
Add table
Reference in a new issue