我是Cython的新手,我想使用Cython来包装一个C/C++静态库.我做了一个简单的例子如下.
Test.h:
#ifndef TEST_H
#define TEST_H
int add(int a, int b);
int multipy(int a, int b);
#endif
Run Code Online (Sandbox Code Playgroud)
TEST.CPP
#include "test.h"
int add(int a, int b)
{
return a+b;
}
int multipy(int a, int b)
{
return a*b;
}
Run Code Online (Sandbox Code Playgroud)
然后我使用g ++来编译和构建它.
g++ -c test.cpp -o libtest.o
ar rcs libtest.a libtest.o
Run Code Online (Sandbox Code Playgroud)
所以现在我得到了一个名为libtest.a的静态库.
Test.pyx:
cdef extern from "test.h":
int add(int a,int b)
int multipy(int a,int b)
print add(2,3)
Run Code Online (Sandbox Code Playgroud)
Setup.py:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = …Run Code Online (Sandbox Code Playgroud) 我对python的类变量有些怀疑.据我所知,如果我定义一个在__init__()函数外声明的类变量,这个变量在C++中只会创建一次作为静态变量.
这似乎适用于某些python类型,例如dict和list类型,但对于那些基类型,例如int,float,则不一样.
例如:
class A:
dict1={}
list1=list()
int1=3
def add_stuff(self, k, v):
self.dict1[k]=v
self.list1.append(k)
self.int1=k
def print_stuff(self):
print self.dict1,self.list1,self.int1
a1 = A()
a1.add_stuff(1, 2)
a1.print_stuff()
a2=A()
a2.print_stuff()
Run Code Online (Sandbox Code Playgroud)
输出是:
{1: 2} [1] 1
{1: 2} [1] 3
Run Code Online (Sandbox Code Playgroud)
我理解dict1和list1的结果,但为什么int1行为不同?
我正在尝试使用正则表达式来提取文件标题中的注释.
例如,源代码可能如下所示:
//This is an example file.
//Please help me.
#include "test.h"
int main() //main function
{
...
}
Run Code Online (Sandbox Code Playgroud)
我想从代码中提取的是前两行,即
//This is an example file.
//Please help me.
Run Code Online (Sandbox Code Playgroud)
任何的想法?
我正在使用Python编写一个正则表达式,用XML节点替换部分字符串.
源字符串如下所示:
Hello REPLACE(str1) this is to replace REPLACE(str2) this is to replace
结果字符串应该是这样的:
Hello <replace name="str1"> this is to replace </replace> <replace name="str2"> this is to replace </replace>
谁能帮我?