使用numba jitclass,其中一个属性是来自单独模块的另一个jitclass

时间:2020-02-07 19:59:59

标签: python numba typing

我有MyMod.py文件

from numba import jitclass                 # import the decorator
from numba import float64, int32, bool_, deferred_type    # import the types
from collections import OrderedDict
import numpy as np

spec = OrderedDict()
spec['a'] = float64
spec['b'] = float64[:]

@jitclass(spec)
class MyClass(object):
    def __init__(self, a, len_b):
        self.a = a
        self.b = np.zeros(len_b, dtype=np.float64)

在MyOtherMod.py中,我有

from numba import jitclass                 # import the decorator
from numba import float64, int32, bool_, deferred_type    # import the types
from collections import OrderedDict
import numpy as np
import MyMod

# spec1 = OrderedDict()
my_inst_type = deferred_type()
my_inst_type.define(MyMod.MyClass.class_type.instance_type)
# spec1['my_inst'] = my_inst_type
spec1 = [('my_inst', my_inst_type)]

@jitclass(spec1)
class MyContainerClass(object):
    def __init__(self, my_inst):
        self.my_inst = my_inst

我尝试将其用于Jupyter笔记本电脑中

import MyMod
import MyOtherMod
import importlib
importlib.reload(MyMod)
importlib.reload(MyOtherMod)

mc_inst = MyMod.MyClass(32.2, 7)

mcc_inst = MyMod.MyContainerClass(mc_inst)

最后一行代码会显示大量错误消息,其中最重要的部分是

File "MyMod.py", line 25:
    def __init__(self, my_inst):
        self.my_inst = my_inst
        ^

[1] During: lowering "(self).my_inst = my_inst" at C:\Users\cbaker2\Documents\Python Scripts\numba_test\MyMod.py (25)
[2] During: resolving callee type: jitclass.MyContainerClass#21843f1a248<my_inst:DeferredType#2303242342216>
[3] During: typing of call at <string> (3)

如果我将两个类都放在同一个文件中,那么这一切都可行。如何在两个单独的模块中使用它?

0 个答案:

没有答案