我正在开发一个jitclass,其中一个方法可以接受int colorBlack = Color.parseColor("#000000");
pieChart.setEntryLabelColor(colorBlack);
,int
或float
的输入参数。我需要能够确定参数是否是数组或其他两种类型。我已尝试使用numpy.ndarray
,如下面的isinstance
方法所示:
interp
但是我收到以下错误:
spec = [('x', float64[:]),
('y', float64[:])]
@jitclass(spec)
class Lookup:
def __init__(self, x, y):
self.x = x
self.y = y
def interp(self, x0):
if isinstance(x0, (float, int)):
result = self._interpolate(x0)
elif isinstance(x0, np.ndarray):
result = np.zeros(x0.size)
for i in range(x0.size):
result[i] = self._interpolate(x0[i])
else:
raise TypeError("`interp` method can only accept types of float, int, or ndarray.")
return result
def _interpolate(self, x0):
x = self.x
y = self.y
if x0 < x[0]:
return y[0]
elif x0 > x[-1]:
return y[-1]
else:
for i in range(len(x) - 1):
if x[i] <= x0 <= x[i + 1]:
x1, x2 = x[i], x[i + 1]
y1, y2 = y[i], y[i + 1]
return y1 + (y2 - y1) / (x2 - x1) * (x0 - x1)
在使用jitclasses或在nopython模式下,有没有办法确定输入参数是否属于某种类型?
我之前应该提到这一点,但使用numba.errors.TypingError: Failed at nopython (nopython frontend)
Failed at nopython (nopython frontend)
Untyped global name 'isinstance': cannot determine Numba type of <class 'builtin_function_or_method'>
File "Lookups.py", line 17
[1] During: resolving callee type: BoundFunction((<class 'numba.types.misc.ClassInstanceType'>, 'interp') for instance.jitclass.Lookup#2167664ca28<x:array(float64, 1d, A),y:array(float64, 1d, A)>)
[2] During: typing of call at <string> (3)
内置也似乎不起作用。例如,如果我将type
方法替换为:
interp
我收到以下错误:
def interp(self, x0):
if type(x0) == float or type(x0) == int:
result = self._interpolate(x0)
elif type(x0) == np.ndarray:
result = np.zeros(x0.size)
for i in range(x0.size):
result[i] = self._interpolate(x0[i])
else:
raise TypeError("`interp` method can only accept types of float, int, or ndarray.")
return result
我认为当我执行类似numba.errors.TypingError: Failed at nopython (nopython frontend)
Failed at nopython (nopython frontend)
Invalid usage of == with parameters (class(int64), Function(<class 'float'>))
之类的操作时,指的是python float
和numba int64
的比较。
答案 0 :(得分:2)
如果你需要确定并比较一个numba jitclass
或nopython jit
函数中的类型,那你就不走运了,因为isinstance
根本不受支持{{1}仅支持几个数字类型和namedtuples(请注意,这只返回类型 - 它不适合比较 - 因为{0}没有为numba函数中的类实现)。
从Numba 0.35开始,唯一受支持的内置插件是(来源:numba documentation):
支持以下内置功能:
type
我的建议:使用普通的Python类并确定其类型,然后转发到==
相应的函数:
abs()
bool
complex
divmod()
enumerate()
float
int: only the one-argument form
iter(): only the one-argument form
len()
min()
max()
next(): only the one-argument form
print(): only numbers and strings; no file or sep argument
range: semantics are similar to those of Python 3 even in Python 2: a range object is returned instead of an array of values.
round()
sorted(): the key argument is not supported
type(): only the one-argument form, and only on some types (e.g. numbers and named tuples)
zip()
答案 1 :(得分:0)
从 numba 0.52 开始,支持 Private WithEvents Thing As Class1 '<~ requires Public Event declaration in Class1.
Private Sub Class_Initialize()
Set Thing = Class1 '<~ will not compile unless Class1 has VB_PredeclaredId=True.
End Sub
。因此,如果您只想区分 swagger: "2.0"
info:
description: "APIs for managing My Site"
version: "0.0.3"
title: "My API"
contact:
email: "email@email.com"
license:
name: "Copyright Amzu IT Ltd"
host: "apihost-xxxx.a.run.app"
x-google-endpoints:
- name: "apihost-xxxx.a.run.app"
allowCors: True
x-google-backend:
address: https://<nodejs-expess-cloud-run-url>.a.run.app
protocol: h2
schemes:
- "https"
paths:
/test:
get:
operationId: Testing
summary: A Test Endpoint to check if the URL is working.
description: A Test Endpoint to check if the URL is working, does not have any security enabled for this URL.
responses:
200:
description: Success
schema:
type: "string"
security:
- key: []
securityDefinitions:
key:
type: apiKey
in: header
name: x-api-key
firebase:
authorizationUrl: ""
flow: "implicit"
type: "oauth2"
x-google-issuer: "https://securetoken.google.com/REMOVED"
x-google-jwks_uri: "https://www.googleapis.com/service_accounts/v1/metadata/x509/securetoken@system.gserviceaccount.com"
x-google-audiences: "REMOVED"
和标量,可以使用以下方法:
np.shape()
np.ndarray
答案 2 :(得分:-1)
使用type()
?
blah = []
if type(blah) is list:
print "Is a list"
blah = 5
if type(blah) is int:
print "we have an int"
即:
>>> blah = 5
>>> type(blah)
<type 'int'>
>>>