我正在为 python 编写 C 扩展。我暂时只是在尝试,我已经编写了一个 hello world 扩展,如下所示:
#include <Python2.7/Python.h>
static PyObject* helloworld(PyObject* self)
{
return Py_BuildValue("s", "Hello, Python extensions!!");
}
static char helloworld_docs[] = "helloworld( ): Any message you want to put here!!\n";
static PyMethodDef helloworld_funcs[] = {
{"helloworld", (PyCFunction)helloworld, METH_NOARGS, helloworld_docs},
{NULL,NULL,0,NULL}
};
void inithelloworld(void)
{
Py_InitModule3("helloworld", helloworld_funcs,"Extension module example!");
}
Run Code Online (Sandbox Code Playgroud)
从我编写的 setup.py 文件安装它并从命令行安装之后,代码工作得很好
python setup.py install
Run Code Online (Sandbox Code Playgroud)
我想要的是以下内容:
我想使用 C 文件作为 python 扩展模块,而不安装它,也就是说,我想将它用作项目中的另一个 python 文件,而不是在我的 python 模块使用其之前需要安装的文件功能。有什么方法可以做到这一点吗?
我一直在阅读这个示例Google样式Python文档字符串文档,以了解如何编写好的Python文档.但我无法理解一件事.
记录字符串时,有一种奇怪的表示法.
例如,在记录参数时,文档指定它们的写法如下:
Args:
arg1(str): The description for arg1
Run Code Online (Sandbox Code Playgroud)
但是,在其他一些地方,该文件写道:
Args:
param2 (:obj:`str`, optional): The second parameter.
Run Code Online (Sandbox Code Playgroud)
在第二种情况下,为什么字符串表示为:obj:`str`而不仅仅是普通的str?为什么首先有两个表示strings?我什么时候使用哪个?
考虑Java中的以下简单类层次结构
class Foo {
protected void someMethod(Bar bar) {
...
}
protected void someOtherMethod(Baz baz) {
...
}
}
class EnhancedFoo extends Foo {
@Override
protected void someMethod(Bar bar) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
我现在开始为这两个类编写JUnit单元测试.由于方法的合同对于someMethod两个类都是相同的,因此我需要两个类基本上完全相同的测试方法(关于someMethod方法),这导致代码重复.使用多个覆盖方法为更丰富的类层次结构执行此操作时,感觉继承对可测试性不利.
此外,即使该方法someOtherMethod没有被覆盖ExtendedFoo,我需要包括相关的测试,ExtendedFoo因为这仍然是合同,这个扩展的类和单元测试应该测试这个.
是否有其他一些在Java中组织层次结构的方法更适合可测试性?是否有一些JUnit构造可以帮助缓解这个问题?
我需要在 Python 中实现一个类,它代表单变量(目前)正态分布。我的想法如下
class Norm():
def __init__(self, mu=0, sigma_sq=1):
self.mu = mu
self.sigma_sq = sigma_sq
# some initialization if necessary
def sample(self):
# generate a sample, where the probability of the value
# of the sample being generated is distributed according
# a normal distribution with a particular mean and variance
pass
N = Norm()
N.sample()
Run Code Online (Sandbox Code Playgroud)
生成的样本应根据以下概率密度函数分布
我知道这一点scipy.stats并Numpy提供了执行此操作的函数,但我需要了解这些函数是如何实现的。任何帮助将不胜感激,谢谢:)
python numpy scipy probability-density probability-distribution
我有一个非常简单的功能,如下所示
def new_price(A, B, x):
return np.linalg.inv(A @ B) @ x
Run Code Online (Sandbox Code Playgroud)
这些是我给它的输入
A = np.array([
[2, 0, 1, 0],
[1, 1, 1, 1],
[0, 0, 0, 10]
])
B = np.array([
[3, 3, 3],
[2, 0, 8],
[0, 5, 3],
[0, 0, 10]
])
x = np.array([ 84, 149, 500])
Run Code Online (Sandbox Code Playgroud)
这将返回数组[ 1. 3. 5.].但是,当我进行以下等式检查时,它会返回False
v1 = new_price(A, B, x)
v2 = np.array([1.0, 3.0, 5.0])
np.array_equal(new_price(A, B, [ 84, 149, 500]), np.array([1.0, 3.0, 5.0]))
Run Code Online (Sandbox Code Playgroud)
我检查了两个阵列的形状和类型是一样的.我在这里错过了什么?
python ×4
numpy ×2
c ×1
java ×1
junit ×1
python-2.7 ×1
python-c-api ×1
scipy ×1
string ×1
unit-testing ×1