Modernize Python 2 for Python 3

This commit is contained in:
Grant Sanderson
2018-07-11 11:38:59 -07:00
parent a5adb90ae8
commit 151a270913
123 changed files with 785 additions and 707 deletions

View File

@ -1,3 +1,4 @@
from __future__ import print_function
from big_ol_pile_of_manim_imports import *
import time
@ -60,16 +61,18 @@ def rev_to_rgba(alpha):
def rev_to_color(alpha):
return rgba_to_color(rev_to_rgba(alpha))
def point_to_rev((x, y), allow_origin = False):
def point_to_rev(xxx_todo_changeme6, allow_origin = False):
# Warning: np.arctan2 would happily discontinuously returns the value 0 for (0, 0), due to
# design choices in the underlying atan2 library call, but for our purposes, this is
# illegitimate, and all winding number calculations must be set up to avoid this
(x, y) = xxx_todo_changeme6
if not(allow_origin) and (x, y) == (0, 0):
print "Error! Angle of (0, 0) computed!"
print("Error! Angle of (0, 0) computed!")
return
return fdiv(np.arctan2(y, x), TAU)
def point_to_size((x, y)):
def point_to_size(xxx_todo_changeme7):
(x, y) = xxx_todo_changeme7
return np.sqrt(x**2 + y**2)
# rescaled_size goes from 0 to 1 as size goes from 0 to infinity
@ -137,8 +140,8 @@ class EquationSolver1d(GraphScene, ZoomedScene):
if self.show_target_line:
self.play(FadeOut(target_line_label)) # Reduce clutter
print "For reference, graphOrigin: ", self.coords_to_point(0, 0)
print "targetYPoint: ", self.coords_to_point(0, self.targetY)
print("For reference, graphOrigin: ", self.coords_to_point(0, 0))
print("targetYPoint: ", self.coords_to_point(0, self.targetY))
# This is a mess right now (first major animation coded),
# but it works; can be refactored later or never
@ -439,10 +442,11 @@ def complex_to_pair(c):
return np.array((c.real, c.imag))
def plane_func_from_complex_func(f):
return lambda (x, y) : complex_to_pair(f(complex(x,y)))
return lambda x_y4 : complex_to_pair(f(complex(x_y4[0],x_y4[1])))
def point3d_func_from_plane_func(f):
def g((x, y, z)):
def g(xxx_todo_changeme):
(x, y, z) = xxx_todo_changeme
f_val = f((x, y))
return np.array((f_val[0], f_val[1], 0))
return g
@ -450,7 +454,8 @@ def point3d_func_from_plane_func(f):
def point3d_func_from_complex_func(f):
return point3d_func_from_plane_func(plane_func_from_complex_func(f))
def plane_zeta((x, y)):
def plane_zeta(xxx_todo_changeme8):
(x, y) = xxx_todo_changeme8
CLAMP_SIZE = 1000
z = complex(x, y)
try:
@ -461,7 +466,8 @@ def plane_zeta((x, y)):
answer = answer/abs(answer) * CLAMP_SIZE
return (float(answer.real), float(answer.imag))
def rescaled_plane_zeta((x, y)):
def rescaled_plane_zeta(xxx_todo_changeme9):
(x, y) = xxx_todo_changeme9
return plane_zeta((x/FRAME_X_RADIUS, 8*y))
# Returns a function from 2-ples to 2-ples
@ -476,13 +482,13 @@ def plane_func_by_wind_spec(*specs):
elif len(p) == 2:
return (p[0], p[1], 1)
else:
print "Error in plane_func_by_wind_spec embiggen!"
print("Error in plane_func_by_wind_spec embiggen!")
specs = map(embiggen, specs)
pos_specs = filter(lambda (x, y, z) : z > 0, specs)
neg_specs = filter(lambda (x, y, z) : z < 0, specs)
pos_specs = filter(lambda x_y_z : x_y_z[2] > 0, specs)
neg_specs = filter(lambda x_y_z1 : x_y_z1[2] < 0, specs)
neg_specs_made_pos = map (lambda (x, y, z) : (x, y, -z), neg_specs)
neg_specs_made_pos = map (lambda x_y_z2 : (x_y_z2[0], x_y_z2[1], -x_y_z2[2]), neg_specs)
def poly(c, root_specs):
return np.prod([(c - complex(x, y))**z for (x, y, z) in root_specs])
@ -651,8 +657,8 @@ class ColorMappedByFuncScene(Scene):
self.input_to_pos_func = lambda p : p
self.pos_to_color_func = self.func
self.pixel_pos_to_color_func = lambda (x, y) : self.pos_to_color_func(
self.num_plane.point_to_coords_cheap(np.array([x, y, 0]))
self.pixel_pos_to_color_func = lambda x_y3 : self.pos_to_color_func(
self.num_plane.point_to_coords_cheap(np.array([x_y3[0], x_y3[1], 0]))
)
jitter_val = 0.1
@ -661,7 +667,7 @@ class ColorMappedByFuncScene(Scene):
def mini_hasher(p):
rgba = point_to_rgba(self.pixel_pos_to_color_func(p))
if rgba[3] != 1.0:
print "Warning! point_to_rgba assigns fractional alpha", rgba[3]
print("Warning! point_to_rgba assigns fractional alpha", rgba[3])
return tuple(rgba)
to_hash = tuple(mini_hasher(p) for p in func_hash_points)
func_hash = hash(to_hash)
@ -674,18 +680,18 @@ class ColorMappedByFuncScene(Scene):
)
self.in_background_pass = not os.path.exists(self.background_image_file)
print "Background file: " + self.background_image_file
print("Background file: " + self.background_image_file)
if self.in_background_pass:
print "The background file does not exist yet; this will be a background creation + video pass"
print("The background file does not exist yet; this will be a background creation + video pass")
else:
print "The background file already exists; this will only be a video pass"
print("The background file already exists; this will only be a video pass")
def construct(self):
if self.in_background_pass:
self.camera.set_background_from_func(
lambda (x, y): point_to_rgba(
lambda x_y: point_to_rgba(
self.pixel_pos_to_color_func(
(x, y)
(x_y[0], x_y[1])
)
)
)
@ -863,7 +869,8 @@ class PiWalkerCircle(PiWalker):
self.walk_coords = [r * np.array((np.cos(i * TAU/N), np.sin(i * TAU/N))) for i in range(N)]
PiWalker.setup(self)
def split_interval((a, b)):
def split_interval(xxx_todo_changeme10):
(a, b) = xxx_todo_changeme10
mid = (a + b)/2.0
return ((a, mid), (mid, b))
@ -915,7 +922,7 @@ class RectangleData():
elif dim == 1:
return_data = [RectangleData(x_interval, new_interval) for new_interval in split_interval(y_interval)[::-1]]
else:
print "RectangleData.splits_on_dim passed illegitimate dimension!"
print("RectangleData.splits_on_dim passed illegitimate dimension!")
return tuple(return_data)
@ -928,7 +935,7 @@ class RectangleData():
elif dim == 1:
sides = (self.get_left(), self.get_right())
else:
print "RectangleData.split_line_on_dim passed illegitimate dimension!"
print("RectangleData.split_line_on_dim passed illegitimate dimension!")
return tuple([mid(x, y) for (x, y) in sides])
@ -973,10 +980,10 @@ class EquationSolver2dNode(object):
def play_in_bfs(self, scene, border_anim):
bfs_nodes = self.hacky_bfs()
print "Number of nodes: ", len(bfs_nodes)
print("Number of nodes: ", len(bfs_nodes))
if len(bfs_nodes) < 1:
print "Less than 1 node! Aborting!"
print("Less than 1 node! Aborting!")
return
scene.play(bfs_nodes[0].first_anim, border_anim)
@ -1029,7 +1036,7 @@ class EquationSolver2d(ColorMappedObjectsScene):
def construct(self):
if self.num_iterations == 0:
print "You forgot to set num_iterations (maybe you meant to subclass something other than EquationSolver2d directly?)"
print("You forgot to set num_iterations (maybe you meant to subclass something other than EquationSolver2d directly?)")
return
ColorMappedObjectsScene.construct(self)
@ -1071,7 +1078,7 @@ class EquationSolver2d(ColorMappedObjectsScene):
def Animate2dSolver(cur_depth, rect, dim_to_split,
sides_to_draw = [0, 1, 2, 3],
manual_wind_override = None):
print "Solver at depth: " + str(cur_depth)
print("Solver at depth: " + str(cur_depth))
if cur_depth >= self.num_iterations:
return EquationSolver2dNode(empty_animation)
@ -1195,7 +1202,7 @@ class EquationSolver2d(ColorMappedObjectsScene):
rect = RectangleData(x_interval, y_interval)
print "Starting to compute anim"
print("Starting to compute anim")
node = Animate2dSolver(
cur_depth = 0,
@ -1205,7 +1212,7 @@ class EquationSolver2d(ColorMappedObjectsScene):
manual_wind_override = self.manual_wind_override
)
print "Done computing anim"
print("Done computing anim")
if self.display_in_parallel:
anim = node.display_in_parallel()
@ -1241,14 +1248,14 @@ class EquationSolver2d(ColorMappedObjectsScene):
rate_func = rect_rate
)
print "About to do the big Play; for reference, the current time is ", time.strftime("%H:%M:%S")
print("About to do the big Play; for reference, the current time is ", time.strftime("%H:%M:%S"))
if self.use_separate_plays:
node.play_in_bfs(self, border_anim)
else:
self.play(anim, border_anim)
print "All done; for reference, the current time is ", time.strftime("%H:%M:%S")
print("All done; for reference, the current time is ", time.strftime("%H:%M:%S"))
self.wait()
@ -1712,7 +1719,7 @@ class Initial2dFuncSceneBase(Scene):
}
def show_planes(self):
print "Error! Unimplemented (pure virtual) show_planes"
print("Error! Unimplemented (pure virtual) show_planes")
def shared_construct(self):
points = [LEFT + DOWN, RIGHT + DOWN, LEFT + UP, RIGHT + UP]
@ -2414,7 +2421,7 @@ class BorderOf2dRegionScene(Scene):
border = Polygon(*points, color = negative_color, stroke_width = border_stroke_width)
self.play(ShowCreation(border))
big_loop_no_zeros_func = lambda (x, y) : complex_to_pair(np.exp(complex(10, y * np.pi)))
big_loop_no_zeros_func = lambda x_y5 : complex_to_pair(np.exp(complex(10, x_y5[1] * np.pi)))
class BigLoopNoZeros(ColorMappedObjectsScene):
CONFIG = {
@ -2539,7 +2546,8 @@ class TinyLoopOfBasicallySameColor(PureColorMap):
self.play(ShowCreation(circle))
self.wait()
def uhOhFunc((x, y)):
def uhOhFunc(xxx_todo_changeme11):
(x, y) = xxx_todo_changeme11
x = -np.clip(x, -5, 5)/5
y = -np.clip(y, -3, 3)/3
@ -2648,7 +2656,8 @@ class UhOhSalientStill(ColorMappedObjectsScene):
# Random test scenes and test functions go here:
def rect_to_circle((x, y, z)):
def rect_to_circle(xxx_todo_changeme12):
(x, y, z) = xxx_todo_changeme12
size = np.sqrt(x**2 + y**2)
max_abs_size = max(abs(x), abs(y))
return fdiv(np.array((x, y, z)) * max_abs_size, size)
@ -2725,7 +2734,7 @@ class SpecifiedWinder(PiWalker):
cur_pos += (self.step_size, 0)
mid_rev = rev_func(cur_pos)
print "Reached ", cur_pos, ", with rev ", mid_rev - start_rev
print("Reached ", cur_pos, ", with rev ", mid_rev - start_rev)
mid_pos = cur_pos.copy()
end_rev = mid_rev
@ -2735,10 +2744,10 @@ class SpecifiedWinder(PiWalker):
end_pos = cur_pos.copy()
print "Reached ", cur_pos, ", with rev ", end_rev - mid_rev
print("Reached ", cur_pos, ", with rev ", end_rev - mid_rev)
self.walk_coords = [start_pos, mid_pos, end_pos]
print "Walk coords: ", self.walk_coords
print("Walk coords: ", self.walk_coords)
PiWalker.setup(self)
class OneFifthTwoFifthWinder(SpecifiedWinder):