【发布时间】:2019-04-04 18:01:41
【问题描述】:
我知道字符串现在由 numba 支持,但是找不到任何关于如何在 jitclass 中使用字符串的文档并且无法弄清楚。
如何使用 jitclass 创建字符串属性?
(这个 hack 是预字符串支持并且相当混乱:How can I pass string type in class in numba jitclass python?)
我已经尝试过 unicode_type、char、char[:]、uint8、str——基本上我能想到的所有东西。
COND_SPEC = [
('feature',nb.unicode_type),
('val', nb.unicode_type)
]
@jitclass(COND_SPEC)
class Cond:
""" Class implementing conditional. """
def __init__(self, feature, val):
self.feature = feature
self.val = val
类编译,但声明类的实例会产生错误:
c = Cond('education','HS-grad')
numba.errors.LoweringError: Failed in nopython mode pipeline (step: nopython mode backend)
Cannot cast unicode_type to int8: %".37" = load {i8*, i64, i32, i64, i8*, i8*}, {i8*, i64, i32, i64, i8*, i8*}* %"feature"
File "<ipython-input-19-aaeb1c1955cb>", line 12:
def __init__(self, feature, val):
self.feature = feature
^
[1] During: lowering "(self).feature = feature" at <ipython-input-19-aaeb1c1955cb> (12)
[2] During: resolving callee type: jitclass.Cond#7f9c36758a18<feature:int8,val:int8>
[3] During: typing of call at <string> (3)
--%<----------------------------------------------------------------------------
File "<string>", line 3:
<source missing, REPL/exec in use?>
【问题讨论】: