我有一个pyx函数,它将获得一个numpy数组。我不知道运行时间之前的尺寸。这很容易检查,但问题是我有一个numpy数组{
"_superBill": {
"$oid": "568b250ba082dfc752b20021"
},
"paymentProviderTxID": "aaaa",
"transactionRaw": "abcdef",
"commissionRate": 0.2,
"visitDate": {
"$date": "2016-12-26T13:04:05.544Z"
}
},
{
"_superBill": {
"$oid": "568b250ba082dfc752b20021"
},
"paymentProviderTxID": "bbbb",
"transactionRaw": "abcdef",
"commissionRate": 0.2,
"visitDate": {
"$date": "2016-12-26T13:04:05.544Z"
}
},
{
"_superBill": null,
"paymentProviderTxID": "cccc",
"transactionRaw": "abcdef",
"commissionRate": 0.2,
"visitDate": {
"$date": "2016-12-27T13:04:05.544Z"
}
},
{
"_superBill": null,
"paymentProviderTxID": "dddd",
"transactionRaw": "abcdef",
"commissionRate": 0.2,
"visitDate": {
"$date": "2016-12-28T13:04:05.544Z"
}
}
与dimesion 1或2.我想检查以设置正确
键入内存视图:
Transaction.aggregate([
{ $match: { paymentDate: { $gte: period.periodStart, $lt: period.periodEnd }} },
{
$group: {
_id: "$_superBill",
visits: { $sum: 1 }
}
},
{
$project: {
_id: '$_id',
superBill: '$_id',
visits: '$visits',
visitDate: '$visitDate',
commissionRate: '$commissionRate'
}
}
], function(err,results) {
// Process results
});
但是我收到一条错误消息,指出此处不允许使用cdef。不明白为什么?
答案 0 :(得分:2)
您正在尝试动态执行类型声明,但Cython和编译器在编译时需要类型声明。编译cython代码时,cython使用您提供的静态类型信息来优化代码。如果类型不是静态的,即您在运行时之前不知道类型或维度,则cython不能在编译时包含该信息。
有两种主要方法可以解决这个问题,一种是为每种可能的类型定义不同的函数,例如:
def myFun1D(double[::1] array):
pass
def myFun2D(double[:, ::1] array):
pass
def foo(array):
cdef double r
if array.ndim == 1:
r = myFun1D(array)
elif array.ndim == 2:
r = myFun2D(array)
第二个选项是不输入declare array,并允许cython将其视为动态类型的python对象。
答案 1 :(得分:0)