Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Cython: How can I import multiple class object from a single file?

$
0
0

So this is how I currently structure my files.

CPP FolderCython Folder├── setup.py└── Geometry├── Circle│├── __init__.py│├── Circle.pyx│├── Circle.pyi│└── Circle.pxd└── Point├── __init__.py├── Point.pyx├── Point.pyi└── Point.pxd 

And this is my setup.py file

from setuptools import setup, Extension, find_packagesfrom Cython.Build import cythonizepoint_extension = Extension("Geometry.Point.Point",    ["src/Geometry/Point/Point.pyx","../cpp/lib/src/Point.cpp"    ],    include_dirs=["../cpp/lib/include"    ],    libraries=["Some Library"],    library_dirs=["src/Geometry"    ],    extra_compile_args=["-std=c++17", "-O3"],    language="c++")circle_extension = Extension("Geometry.Circle.Circle",    ["../cpp/lib/src/Circle.cpp","src/Geometry/Circle/Circle.pyx","../cpp/lib/src/Point.cpp"    ],    include_dirs=["../cpp/lib/include"    ],    libraries=["Some Library"],    library_dirs=["src/Geometry"    ],    extra_compile_args=["-std=c++17", "-O3"],    language="c++")setup(    name="Geometry",    version="0.1",    packages=find_packages(where="src"),    package_dir={"": "src"},    package_data={"Geometry.Circle": ["*.so", "*.pyi"],"Geometry.Point": ["*.so", "*.pyi"]    },    ext_modules=cythonize([point_extension, circle_extension],                          compiler_directives={"language_level": 3},                          include_path=["../../Expression/cython/src/Some Library","src/Geometry",                          ],                          annotate=True),    zip_safe=False,)

With this setup, when I wanna import Circle or Point for testing, I have to do as below:

from Geometry.Point import Pointfrom Geometry.Circle import Circle

And my goal is to be able to import them in this way: from Geometry import Circle, Point

So I think i should structure my file as follow:

CPP FolderCython Folder├── setup.py└── Geometry├── __init__.py├── Geometry.pyx├── Geometry.pyi├── Geometry.pxd├── Circle│├── __init__.py│├── Circle.pyx│├── Circle.pyi│└── Circle.pxd└── Point├── __init__.py├── Point.pyx├── Point.pyi└── Point.pxd 

How should I rewrite my setup.py and write my Geometry.pxd, .pyx and .pyi?FYI this is a sample of my Point.pxd and Point.pyx

**[Point.pxd]**from libcpp.memory cimport shared_ptr, weak_ptr, make_sharedfrom Bryan cimport _Bryan, Bryancdef extern from "Point.h":    cdef cppclass _Point:        _Point(shared_ptr[_Bryan] x, shared_ptr[_Bryan] y, shared_ptr[_Bryan] z)        shared_ptr[_Bryan] get_x()        shared_ptr[_Bryan] get_y()        shared_ptr[_Bryan] get_z()cdef class Point:    cdef shared_ptr[_Point] c_point**[Point.pyx]**from Point cimport *cdef class Point:    def __cinit__(self, Bryan x=Bryan("0", None), Bryan y=Bryan("0", None), Bryan z=Bryan("0", None)):        self.c_point = make_shared[_Point](x.thisptr, y.thisptr, z.thisptr)    def __dealloc(self):        self.c_point.reset()    def get_x(self) -> Bryan:        cdef shared_ptr[_Bryan] result = self.c_point.get().get_x()        cdef Bryan coord = Bryan("", None, make_with_pointer = True)        coord.thisptr = result        return coord    def get_y(self) -> Bryan:        cdef shared_ptr[_Bryan] result = self.c_point.get().get_y()        cdef Bryan coord = Bryan("", None, make_with_pointer = True)        coord.thisptr = result        return coord    def get_z(self) -> Bryan:        cdef shared_ptr[_Bryan] result = self.c_point.get().get_z()        cdef Bryan coord = Bryan("", None, make_with_pointer = True)        coord.thisptr = result        return coord    property x:        def __get__(self):            return self.get_x()    property y:        def __get__(self):            return self.get_y()    property z:        def __get__(self):            return self.get_z()

Thank you


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>