> debugfile('C:/some_folder/convnet.py', wdir=r'C:/some_folder')
> debugfile('C:/some_folder/convnet.py', args='--epochs=300 --data-path=G:\zsh\data\cifar-10-py-colmajor --save-path=G:\zsh\2paper\source-code\save --test-range=6', wdir=r'C:/some_folder')
Suppose you have a simple C++ example class you want to talk to in a file called foo.cpp:
#include <iostream>
class Foo{
public:
void bar(){
std::cout << "Hello" << std::endl;
}
};
Since ctypes can only talk to C functions, you need to provide those declaring them as extern "C"
extern "C" {
Foo* Foo_new(){ return new Foo(); }
void Foo_bar(Foo* foo){ foo->bar(); }
}
Next you have to compile this to a shared library
g++ -c -fPIC foo.cpp -o foo.o
g++ -shared -Wl,-soname,libfoo.so -o libfoo.so foo.o
And finally you have to write your python wrapper (e.g. in fooWrapper.py)
from ctypes import cdll
lib = cdll.LoadLibrary('./libfoo.so')
class Foo(object):
def __init__(self):
self.obj = lib.Foo_new()
def bar(self):
lib.Foo_bar(self.obj)
Once you have that you can call it like
f = Foo()
f.bar() #and you will see "Hello" on the screen
class Person:
def __init__(self, first, last, age):
self.firstname = first
self.lastname = last
self.age = age
def __str__(self):
return self.firstname + " " + self.lastname + ", " + str(self.age)
def sum(self, a, b):
return (a * b)
class Employee(Person):
def __init__(self, first, last, age, staffnum):
Person.__init__(self, first, last, age)
self.staffnumber = staffnum
def __str__(self):
return Person.__str__(self) + ", " + self.staffnumber
def sum(self, a, b):
return (a + b)
def f(self, n, m = None):
if m is not None:
return n + m + 62
else:
return n + 62
x = Person("Marge", "Simpson", 36)
y = Employee("Homer", "Simpson", 28, "1007")
print(x)
print(y)
print x.sum(10, 2)
print y.sum(10, 2)
print y.f(1.23, 2)
参考网址: