CPPYY include 和 load_libraries及动

作者: 北方的花姑娘 | 来源:发表于2019-08-24 23:53 被阅读0次

    CPPYY Part II

    动态特性

    我们来实现一个简单的KNN算法

    """
    新建一个文件 nearest_neighbors.h
    ------------------
    #pragma once
    
    #include <iostream>
    #include <vector>
    
    class Point {
    public:
      Point() {};
      Point(double x, double y) : x(x), y(y) {};
      double x, y;
    };
    
    class NearestNeighbors {
     public:
      NearestNeighbors() {};
      std::vector<Point> points;
      std::vector<Point> nearest(Point, int k);
    };
    ------------------
    """
    # 将头文件include 进来
    cppyy.include("nearest_neighbors.h")
    
    # 现在已经可以使用 Point 和 NearestNeighbors 类了
    Point = cppyy.gbl.Point
    KNN = cppyy.gbl.NearestNeighbors
    
    # 构造一个 Point
    p = Point(1.0, 2.0)
    print(repr(p))
    
    <cppyy.gbl.Point object at 0x000001E120E918B0>
    
    # 我们希望print(p) 可以看到具体的信息
    Point.__repr__ = lambda self: "<Point object at 0x{:04X}, x: {}, y: {}>".format(id(self), self.x, self.y)
    
    # 再次print(p)
    print(repr(p))  # >> p   # in ipython
    
    <Point object at 0x1E122C5CB40, x: 1.0, y: 2.0>
    
    # 现在初始化 KNN,需要一系列的Points
    from random import random
    points = [Point(random()*10, random()*10) for _ in range(10)]
    knn = KNN()
    knn.points = Vector[Point](points)
    
    list(knn.points)
    
    [<Point object at 0x1E122EC5CA8, x: 8.423026486604515, y: 4.205179067516682>,
     <Point object at 0x1E1217F8438, x: 1.38495460424919, y: 8.902270221641716>,
     <Point object at 0x1E122FB5870, x: 5.4137809335642615, y: 5.081185223495259>,
     <Point object at 0x1E122FB56C0, x: 4.43062339130408, y: 0.3717755170621895>,
     <Point object at 0x1E122FB5990, x: 7.143169297375893, y: 7.112832543272915>,
     <Point object at 0x1E122FB5B88, x: 2.619781100077131, y: 2.0009995637839486>,
     <Point object at 0x1E122FB55E8, x: 9.678854624172509, y: 7.42921725146636>,
     <Point object at 0x1E122FB53F0, x: 9.058931286852678, y: 6.025951815850337>,
     <Point object at 0x1E122FB5FC0, x: 1.2802522942068906, y: 3.7686758142413557>,
     <Point object at 0x1E122FB5BD0, x: 1.5544746354777161, y: 1.2622207703902388>]
    
    p = Point(2.4, 3.5)
    
    # 现在可以直接调用下面的knn.nearest(p, 2)吗?
    # 答案是不行的,因为只include了头文件,knn.nearest函数是未定义的,如果调用将会报错
    knn.nearest(p, 2)
    -------------------------------------------------
    IncrementalExecutor::executeFunction: symbol '?nearest@NearestNeighbors@@QEAA?AV?$vector@VPoint@@V?$allocator@VPoint@@@std@@@std@@VPoint@@H@Z' unresolved while linking symbol '__cf_17'!
    You are probably missing the definition of public: class std::vector<class Point,class std::allocator<class Point> > __cdecl NearestNeighbors::nearest(class Point,int) __ptr64
    Maybe you need to load the corresponding shared library?
        ValueError                                Traceback (most recent call last)
        <ipython-input-55-44717562f51d> in <module>
              1 # 现在可以直接调用下面的knn.nearest(p, 2)吗?
              2 # 答案是不行的,因为只include了头文件,knn.nearest函数是未定义的,如果调用将会报错
        ----> 3 knn.nearest(p, 2)
       
        ValueError: vector<Point> NearestNeighbors::nearest(Point, int k) =>
        ValueError: nullptr result where temporary expected
    
    # 需要将包含该函数定义的动态库文件导入进来
    # knn_lib.dll 为用MSVC编译的,可能是符号不一致,还是会报错,在linux下导入gcc编译的.so文件以后,是确定可行的
    cppyy.load_library("knn_lib.dll")
    
    # 此时应该输出正确的结果
    knn.nearest(p, 2)
    

    总结

    • cppyy生成的python 类可以很方便的修改函数实现等
    • 使用cppyy.include 与 cppyy.load_libraries来实现导入c++模块

    相关文章

      网友评论

        本文标题:CPPYY include 和 load_libraries及动

        本文链接:https://www.haomeiwen.com/subject/qomcectx.html