1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Python: Numpy shape typing

Discussão em 'Python' iniciado por Stack, Fevereiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have a class with one method:

    import numpy as np
    import numpy.typing as npt
    import pyvista as pv
    from typing import Tuple, TypeVar, Literal


    class SurfaceTriangleMesh:

    int_type = TypeVar("int_type", bound=int)
    flt_type = TypeVar("flt_type", bound=float)
    int_Shape2D = Tuple[int_type, int_type]
    flt_Shape2D = Tuple[flt_type, flt_type]
    int_Shape2DType = TypeVar("int_Shape2DType", bound=int_Shape2D)
    flt_Shape2DType = TypeVar("flt_Shape2DType", bound=flt_Shape2D)

    def __init__(self, mesh: pv.PolyData):
    self.mesh = mesh

    def add_new_points(self, points_to_add: npt.NDArray[flt_Shape2DType]):
    """
    Add new points to surface mesh from attribute self.mesh
    :param points_to_add: points-to-add array of shape [N * 3] of type float
    :return: new surface mesh of type pyvista.PolyData
    """
    pass


    In the method add_new_points I want to pass numpy array of shape [*, 3] and I want to check the shape of this array before to proceed with operations. Is there any way of "automatic" shape assertion?
    I.e., can I use some classes or side-libs allowing to assign shape and type of a numpy array in the way when its shape and type are asserted automatically inside classes of these side-libs, so I don't need to write assertions by my own

    Or I need to write assertions manually?

    Continue reading...

Compartilhe esta Página