Skip to content

Use lazy imports in topology to speed up library import. #942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/build123d/exporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
# pylint: disable=no-name-in-module, import-error
# pylint: disable=too-many-lines

from __future__ import annotations
from typing import TYPE_CHECKING
import math
import xml.etree.ElementTree as ET
from copy import copy
Expand All @@ -41,7 +43,6 @@
from collections.abc import Callable, Iterable

import ezdxf
import svgpathtools as PT
from ezdxf import zoom
from ezdxf.colors import RGB, aci2rgb
from ezdxf.math import Vec2
Expand Down Expand Up @@ -69,8 +70,10 @@
)
from build123d.build_common import UNITS_PER_METER

PathSegment: TypeAlias = PT.Line | PT.Arc | PT.QuadraticBezier | PT.CubicBezier
"""A type alias for the various path segment types in the svgpathtools library."""
if TYPE_CHECKING:
import svgpathtools as PT # pylint: disable=C0415
PathSegment: TypeAlias = PT.Line | PT.Arc | PT.QuadraticBezier | PT.CubicBezier
"""A type alias for the various path segment types in the svgpathtools library."""

# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
Expand Down
5 changes: 3 additions & 2 deletions src/build123d/importers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@
XCAFDoc_ColorSurf,
XCAFDoc_DocumentTool,
)
from ocpsvg import ColorAndLabel, import_svg_document
from svgpathtools import svg2paths

from build123d.build_enums import Align
from build123d.geometry import Color, Location, Vector, to_align_offset
Expand Down Expand Up @@ -270,6 +268,7 @@ def import_svg_as_buildline_code(
Returns:
tuple[str, str]: code, builder instance name
"""
from svgpathtools import svg2paths # pylint: disable=C0415

translator = {
"Line": ["Line", "start", "end"],
Expand Down Expand Up @@ -361,6 +360,8 @@ def import_svg(
Returns:
ShapeList[Union[Wire, Face]]: objects contained in svg
"""
from ocpsvg import ColorAndLabel, import_svg_document # pylint: disable=C0415

if is_inkscape_label is not None: # TODO remove for `1.0` release
msg = "`is_inkscape_label` parameter is deprecated"
if is_inkscape_label:
Expand Down
3 changes: 2 additions & 1 deletion src/build123d/objects_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

import copy as copy_module
from math import copysign, cos, radians, sin, sqrt
from scipy.optimize import minimize

from collections.abc import Iterable

Expand Down Expand Up @@ -204,6 +203,8 @@ def __init__(
keep: Keep = Keep.TOP,
mode: Mode = Mode.ADD,
):
from scipy.optimize import minimize # pylint: disable=C0415

context: BuildLine | None = BuildLine._get_context(self)
validate_inputs(context, self)

Expand Down
3 changes: 2 additions & 1 deletion src/build123d/operations_sketch.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from __future__ import annotations

from collections.abc import Iterable
from scipy.spatial import Voronoi
from build123d.build_enums import Mode, SortBy
from build123d.topology import (
Compound,
Expand Down Expand Up @@ -76,6 +75,8 @@ def full_round(
geometric center of the arc, and the third the radius of the arc

"""
from scipy.spatial import Voronoi # pylint: disable=C0415

context: BuildSketch | None = BuildSketch._get_context("full_round")

if not isinstance(edge, Edge):
Expand Down
6 changes: 4 additions & 2 deletions src/build123d/topology/one_d.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@
from typing import Literal, overload, TYPE_CHECKING
from typing_extensions import Self
from numpy import ndarray
from scipy.optimize import minimize
from scipy.spatial import ConvexHull

import OCP.TopAbs as ta
from OCP.BRep import BRep_Tool
Expand Down Expand Up @@ -1972,6 +1970,8 @@ def intersect(
def param_at_point(self, point: VectorLike) -> float:
"""Normalized parameter at point along Edge"""

from scipy.optimize import minimize # pylint: disable=C0415

# Note that this search algorithm would ideally be replaced with
# an OCP based solution, something like that which is shown below.
# However, there are known issues with the OCP methods for some
Expand Down Expand Up @@ -2438,6 +2438,8 @@ def make_convex_hull(cls, edges: Iterable[Edge], tolerance: float = 1e-3) -> Wir
Returns:
Wire: convex hull perimeter
"""
from scipy.spatial import ConvexHull # pylint: disable=C0415

# pylint: disable=too-many-branches, too-many-locals
# Algorithm:
# 1) create a cloud of points along all edges
Expand Down
10 changes: 5 additions & 5 deletions src/build123d/topology/shape_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@
TypeVar,
Union,
overload,
Literal,
TYPE_CHECKING,
)
from typing_extensions import Self

from collections.abc import Callable, Iterable, Iterator

import OCP.GeomAbs as ga
import OCP.TopAbs as ta
from IPython.lib.pretty import pretty, RepresentationPrinter
from OCP.BOPAlgo import BOPAlgo_GlueEnum
from OCP.BRep import BRep_Tool
from OCP.BRepAdaptor import BRepAdaptor_Curve, BRepAdaptor_Surface
Expand Down Expand Up @@ -145,10 +146,6 @@
VectorLike,
logger,
)
from typing_extensions import Self

from typing import Literal


if TYPE_CHECKING: # pragma: no cover
from .zero_d import Vertex # pylint: disable=R0801
Expand All @@ -157,6 +154,7 @@
from .three_d import Solid # pylint: disable=R0801
from .composite import Compound # pylint: disable=R0801
from build123d.build_part import BuildPart # pylint: disable=R0801
from IPython.lib.pretty import RepresentationPrinter

Shapes = Literal["Vertex", "Edge", "Wire", "Face", "Shell", "Solid", "Compound"]
TrimmingTool = Union[Plane, "Shell", "Face"]
Expand Down Expand Up @@ -2266,6 +2264,8 @@ def __repr__(self):
return repr(ShapeList(self))

def __str__(self):
from IPython.lib.pretty import pretty # pylint: disable=C0415

return pretty(self)

def group(self, key: K):
Expand Down
Loading