Skip to content

Commit

Permalink
Fixed imports and cleaned up some type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKempen committed Feb 11, 2024
1 parent 892a1ed commit d9972a5
Show file tree
Hide file tree
Showing 19 changed files with 110 additions and 59 deletions.
1 change: 1 addition & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"redhat.vscode-yaml",
"yzhang.markdown-all-in-one",
"ms-toolsai.jupyter",
"ms-python.black-formatter",
"ms-python.python"
]
}
Expand Down
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,5 @@ poetry.toml

# End of https://www.toptal.com/developers/gitignore/api/python,jupyternotebooks,visualstudiocode


# Don't commit the rendered media files. Still want to keep any source
# media files, though- only get rid of the "media" folder, which is where
# manim renders to.
# Don't commit the rendered media files
media
9 changes: 9 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"recommendations": [
"streetsidesoftware.code-spell-checker",
"redhat.vscode-yaml",
"yzhang.markdown-all-in-one",
"ms-python.black-formatter",
"ms-python.python"
]
}
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
],
"python.analysis.typeCheckingMode": "basic",
"python.analysis.autoImportCompletions": true,
"python.defaultInterpreterPath": "/usr/local/bin/python",
"python.defaultInterpreterPath": "/usr/bin/python",
"python.languageServer": "Pylance",
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
Expand Down
2 changes: 2 additions & 0 deletions build.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#!/bin/bash
# A trivial wrapper for build.py
source .venv/bin/activate
exec "python" "-m" "build" "$@"
2 changes: 1 addition & 1 deletion extensions/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def size(argument: str):

class Animation(sphinx_docutils.SphinxDirective):
"""Animation directive.
Wrapper for the html <video> tag embeding all the supported options
Wrapper for the html <video> tag embeding all the supported options.
"""

# enable content in the directive
Expand Down
19 changes: 13 additions & 6 deletions rc_lib/design/plate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from rc_lib.style import color
from rc_lib.math_utils import tangent, vector
from rc_lib.design import sketch
from rc_lib.utils.type_utils import not_none


class PlateCircle(sketch.Circle):
Expand Down Expand Up @@ -101,16 +102,22 @@ def _make_boundary_lines(self) -> list[sketch.Line]:
]

def draw_inner_circles(self) -> mn.Animation:
return mn.Succession(
*[mn.GrowFromCenter(x.inside) for x in self._entities], lag_ratio=0.75
return not_none(
mn.Succession(
*[mn.GrowFromCenter(x.inside) for x in self._entities], lag_ratio=0.75
)
)

def draw_outer_circles(self) -> mn.Animation:
return mn.Succession(
*[mn.GrowFromCenter(x.outside) for x in self._entities], lag_ratio=0.75
return not_none(
mn.Succession(
*[mn.GrowFromCenter(x.outside) for x in self._entities], lag_ratio=0.75
)
)

def draw_boundary(self) -> mn.Animation:
return mn.Succession(
*[mn.Create(line) for line in self._boundary_lines], lag_ratio=1
return not_none(
mn.Succession(
*[mn.Create(line) for line in self._boundary_lines], lag_ratio=1
)
)
56 changes: 34 additions & 22 deletions rc_lib/design/sketch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Defines entities which look like Onshape sketch entities.
"""

from __future__ import annotations

from typing import Callable, Self, Any
Expand All @@ -11,6 +12,7 @@

from rc_lib.math_utils import vector
from rc_lib.style import color, animation
from rc_lib.utils.type_utils import not_none


class SketchState(color.Color, enum.Enum):
Expand Down Expand Up @@ -86,9 +88,7 @@ def midpoint_constraint(
elif points is None:
raise ValueError("Expected a line or two points.")

return self.animate.move_to(
(points[0].get_center() + points[1].get_center()) / 2
) # type: ignore
return self.animate.move_to((points[0].get_center() + points[1].get_center()) / 2) # type: ignore

def align_constraint(self, target: Point, type: AlignType) -> mn.Animation:
if type == AlignType.VERTICAL:
Expand Down Expand Up @@ -191,18 +191,22 @@ def is_start_closer_to_target(self, target: ArcBase) -> bool:
def _create_override(self) -> mn.Animation:
end = self.get_end()
self.move_end(self.get_start() + vector.ZERO_LENGTH_VECTOR)
return mn.Succession(
animation.Add(self.line),
self.animate(introducer=True).move_end(end), # type: ignore
return not_none(
mn.Succession(
animation.Add(self.line),
self.animate(introducer=True).move_end(end),
)
)

@override
@mn.override_animation(mn.Uncreate)
def _uncreate_override(self) -> mn.Animation:
start = self.get_start() + vector.ZERO_LENGTH_VECTOR
return mn.Succession(
self.animate(remover=True).move_end(start), # type: ignore
animation.Remove(self.line),
return not_none(
mn.Succession(
self.animate(remover=True).move_end(start),
animation.Remove(self.line),
)
)


Expand Down Expand Up @@ -233,7 +237,7 @@ def _set_radius_override(self, radius: float, anim_args={}) -> mn.Animation:
**anim_args,
)
self.arc.radius = radius
return animation
return not_none(animation)

@override
def click_target(self) -> mn.VMobject:
Expand Down Expand Up @@ -276,16 +280,20 @@ def get_group(self) -> mn.VGroup:

@mn.override_animation(mn.Create)
def _create_override(self) -> mn.Animation:
return mn.Succession(
animation.Add(self.middle),
mn.GrowFromCenter(self.arc),
return not_none(
mn.Succession(
animation.Add(self.middle),
mn.GrowFromCenter(self.arc),
)
)

@mn.override_animation(mn.Uncreate)
def _uncreate_override(self) -> mn.Animation:
return mn.Succession(
mn.GrowFromCenter(self.arc, reverse_rate_function=True, remover=True),
animation.Remove(self.middle),
return not_none(
mn.Succession(
mn.GrowFromCenter(self.arc, reverse_rate_function=True, remover=True),
animation.Remove(self.middle),
)
)


Expand All @@ -311,16 +319,20 @@ def get_group(self) -> mn.VGroup:

@mn.override_animation(mn.Create)
def _create_override(self) -> mn.Animation:
return mn.Succession(
animation.Add(self.start, self.end, self.middle),
mn.GrowFromCenter(self.arc),
return not_none(
mn.Succession(
animation.Add(self.start, self.end, self.middle),
mn.GrowFromCenter(self.arc),
)
)

@mn.override_animation(mn.Uncreate)
def _uncreate_override(self) -> mn.Animation:
return mn.Succession(
mn.GrowFromCenter(self.arc, reverse_rate_function=True, remover=True),
animation.Remove(self.start, self.end, self.middle),
return not_none(
mn.Succession(
mn.GrowFromCenter(self.arc, reverse_rate_function=True, remover=True),
animation.Remove(self.start, self.end, self.middle),
)
)


Expand Down
8 changes: 6 additions & 2 deletions rc_lib/design/sketch_animation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import cast
import manim as mn
from rc_lib.design import sketch
from rc_lib.style import color
Expand All @@ -10,7 +11,7 @@
class Click(mn.Transform):
"""Defines an animation which represents an object getting clicked."""

def __init__(self, mobject: sketch.Base) -> None:
def __init__(self, mobject: sketch.Base):
base = mobject.click_target()
target = base.copy().set_stroke(width=4 * 3.5).set_color(color.Palette.YELLOW) # type: ignore

Expand All @@ -29,4 +30,7 @@ def make(animation: mn.Animation, *mobjects: sketch.Base) -> mn.Succession:
Each mobject is clicked in sequence, followed by the final animation playing.
"""
return mn.Succession(*[Click(mobject) for mobject in mobjects], animation)
return cast(
mn.Succession,
mn.Succession(*[Click(mobject) for mobject in mobjects], animation),
)
File renamed without changes.
File renamed without changes.
13 changes: 8 additions & 5 deletions rc_lib/common_mobjects/layouts.py → rc_lib/mobjects/layouts.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""
Mobject groups with available layout methods.
"""
from typing import Callable, List, Any, Self, Tuple, cast

from typing import Callable, List, Any, Self, cast

import manim as mn
from rc_lib.math_utils import vector, mobject_geometry
from rc_lib.common_mobjects import containers
from rc_lib.mobjects import containers
from rc_lib.utils.type_utils import not_none


def edge_from_center(
Expand Down Expand Up @@ -217,8 +219,9 @@ def arrange(self, positions: List[vector.Point2d] | None = None) -> Self:

def animate_arrange(
self,
anim_function: Callable[[mn.VMobject, vector.Point2d], mn.Animation]
| None = None,
anim_function: (
Callable[[mn.VMobject, vector.Point2d], mn.Animation] | None
) = None,
positions: List[vector.Point2d] | None = None,
) -> List[mn.Animation]:
"""
Expand All @@ -236,7 +239,7 @@ def animate_arrange(
def _move_mob_to_pos(
mobject: mn.VMobject, pos: vector.Vector2d
) -> mn.Animation:
return mn.Transform(mobject, mobject.move_to(pos))
return not_none(mn.Transform(mobject, mobject.move_to(pos)))

if positions is None:
positions = self.predict_arrangement()
Expand Down
12 changes: 6 additions & 6 deletions rc_lib/style/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
# Which would have the affect of adding the module `color` to `__all__`.
# OR, instead of doing that manually, we can do it automatically:
# (kudos to https://stackoverflow.com/a/3365846/10717280)
import pkgutil
# import pkgutil

__all__ = []
for loader, module_name, is_pkg in pkgutil.walk_packages(__path__):
__all__.append(module_name)
_module = loader.find_module(module_name).load_module(module_name)
globals()[module_name] = _module
# __all__ = []
# for loader, module_name, is_pkg in pkgutil.walk_packages(__path__):
# __all__.append(module_name)
# _module = loader.find_module(module_name).load_module(module_name)
# globals()[module_name] = _module
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import manim as mn
from rc_lib.style import color, text
from rc_lib.utils.type_utils import not_none


class TitleSequence:
Expand Down Expand Up @@ -28,9 +29,9 @@ def next(self, title: str, color: color.Color | None = None) -> mn.Animation:
self._number += 1
if self._number == 2:
self._first = text
return mn.Write(self._first, run_time=0.75)
return not_none(mn.Write(self._first, run_time=0.75))
else:
return mn.Transform(self._first, text, run_time=0.75)
return not_none(mn.Transform(self._first, text, run_time=0.75))

def _make_text(self, title: str, color: color.Color) -> mn.Text:
prefix = str(self._number) + ". " if self._add_numbers else ""
Expand Down
10 changes: 10 additions & 0 deletions rc_lib/utils/type_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from typing import TypeVar


T = TypeVar("T")


def not_none(value: T | None) -> T:
if value is None:
raise AssertionError("Unexpected null")
return value
12 changes: 6 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# website
Sphinx==6.1.3
sphinx-copybutton==0.5.1
sphinx-rtd-theme==1.2.0
Sphinx>=6.1.3
sphinx-copybutton>=0.5.1
sphinx-rtd-theme>=1.2.0
myst-parser>=1.0.0

# animation libraries
manim>=0.18
numpy>=1.24.2
typing-extensions>=4.5.0
setuptools
# numpy>=1.24.2

# jupyter and formatting
ipykernel>=6.21.3
black>=23.1.0
# ipykernel>=6.21.3

# build script
thefuzz[speedup]>=0.19.0
11 changes: 8 additions & 3 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#!/bin/bash

# install dependencies
# install manim dependencies
sudo apt-get update
# manim system libraries
sudo apt-get install -y build-essential libcairo2-dev libpango1.0-dev ffmpeg # texlive texlive-latex-extra
sudo apt-get install -y build-essential python3.12-dev libcairo2-dev libpango1.0-dev ffmpeg
# texlive texlive-latex-extra

# Pillow dependencies
sudo apt-get install libtiff5-dev libjpeg8-dev libopenjp2-7-dev zlib1g-dev \
libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python3-tk \
libharfbuzz-dev libfribidi-dev libxcb1-dev

# install pip requirements
pip install -r requirements.txt
Expand Down
2 changes: 1 addition & 1 deletion website/design/plate/plate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import manim as mn
from rc_lib.style import color, animation
from rc_lib.math_utils import vector
from rc_lib.view_utils import title_sequence
from rc_lib.utils import title_sequence
from rc_lib.design import plate, sketch, constraint

inner_color: color.Color = color.Palette.GREEN
Expand Down

0 comments on commit d9972a5

Please sign in to comment.