mirror of
https://github.com/3b1b/manim.git
synced 2025-07-30 13:34:19 +08:00
Changed all files to (mostly) conform to PEP8
This commit is contained in:
@ -1,14 +1,16 @@
|
||||
import inspect
|
||||
import operator as op
|
||||
|
||||
|
||||
def instantiate(obj):
|
||||
"""
|
||||
Useful so that classes or instance of those classes can be
|
||||
Useful so that classes or instance of those classes can be
|
||||
included in configuration, which can prevent defaults from
|
||||
getting created during compilation/importing
|
||||
"""
|
||||
return obj() if isinstance(obj, type) else obj
|
||||
|
||||
|
||||
def get_all_descendent_classes(Class):
|
||||
awaiting_review = [Class]
|
||||
result = []
|
||||
@ -18,6 +20,7 @@ def get_all_descendent_classes(Class):
|
||||
result.append(Child)
|
||||
return result
|
||||
|
||||
|
||||
def filtered_locals(caller_locals):
|
||||
result = caller_locals.copy()
|
||||
ignored_local_args = ["self", "kwargs"]
|
||||
@ -25,12 +28,13 @@ def filtered_locals(caller_locals):
|
||||
result.pop(arg, caller_locals)
|
||||
return result
|
||||
|
||||
def digest_config(obj, kwargs, caller_locals = {}):
|
||||
|
||||
def digest_config(obj, kwargs, caller_locals={}):
|
||||
"""
|
||||
Sets init args and CONFIG values as local variables
|
||||
|
||||
The purpose of this function is to ensure that all
|
||||
configuration of any object is inheritable, able to
|
||||
The purpose of this function is to ensure that all
|
||||
configuration of any object is inheritable, able to
|
||||
be easily passed into instantiation, and is attached
|
||||
as an attribute of the object.
|
||||
"""
|
||||
@ -44,16 +48,17 @@ def digest_config(obj, kwargs, caller_locals = {}):
|
||||
if hasattr(Class, "CONFIG"):
|
||||
static_configs.append(Class.CONFIG)
|
||||
|
||||
#Order matters a lot here, first dicts have higher priority
|
||||
# Order matters a lot here, first dicts have higher priority
|
||||
caller_locals = filtered_locals(caller_locals)
|
||||
all_dicts = [kwargs, caller_locals, obj.__dict__]
|
||||
all_dicts += static_configs
|
||||
all_new_dicts = [kwargs, caller_locals] + static_configs
|
||||
obj.__dict__ = merge_config(all_dicts)
|
||||
#Keep track of the configuration of objects upon
|
||||
#instantiation
|
||||
# Keep track of the configuration of objects upon
|
||||
# instantiation
|
||||
obj.initial_config = merge_config(all_new_dicts)
|
||||
|
||||
|
||||
def merge_config(all_dicts):
|
||||
all_config = reduce(op.add, [d.items() for d in all_dicts])
|
||||
config = dict()
|
||||
@ -62,11 +67,12 @@ def merge_config(all_dicts):
|
||||
if not key in config:
|
||||
config[key] = value
|
||||
else:
|
||||
#When two dictionaries have the same key, they are merged.
|
||||
# When two dictionaries have the same key, they are merged.
|
||||
if isinstance(value, dict) and isinstance(config[key], dict):
|
||||
config[key] = merge_config([config[key], value])
|
||||
return config
|
||||
|
||||
|
||||
def soft_dict_update(d1, d2):
|
||||
"""
|
||||
Adds key values pairs of d2 to d1 only when d1 doesn't
|
||||
@ -76,7 +82,8 @@ def soft_dict_update(d1, d2):
|
||||
if key not in d1:
|
||||
d1[key] = value
|
||||
|
||||
def digest_locals(obj, keys = None):
|
||||
|
||||
def digest_locals(obj, keys=None):
|
||||
caller_locals = filtered_locals(
|
||||
inspect.currentframe().f_back.f_locals
|
||||
)
|
||||
@ -85,31 +92,10 @@ def digest_locals(obj, keys = None):
|
||||
for key in keys:
|
||||
setattr(obj, key, caller_locals[key])
|
||||
|
||||
# Occasionally convenient in order to write dict.x instead of more laborious
|
||||
# Occasionally convenient in order to write dict.x instead of more laborious
|
||||
# (and less in keeping with all other attr accesses) dict["x"]
|
||||
|
||||
|
||||
class DictAsObject(object):
|
||||
def __init__(self, dict):
|
||||
self.__dict__ = dict
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
self.__dict__ = dict
|
||||
|
Reference in New Issue
Block a user