How can I pass string type in class in numba jitclass python?

a 夏天 提交于 2019-12-11 04:38:54

问题


while I added this before my class but I noticed that I passed a string to my class when I neglected to declare it for jitclass can't work and when i trying using string to be the same type can't use it.

spec = [
    ('filename', str),
    ('rows', int32),
    ('cls', int32),
    ('L', int32),
    ('H', int32),
    ('checking', int32[:]),
    ('enum_file', int32[:]),
    ('step', int32),
    ('slices', int32),
]

@jitclass(spec)

TypeError: spec values should be Numba type instances, got <class 'str'> ..........................................................................


回答1:


How about this? It's a little ugly, but might be a good enough workaround:

import numba as nb

spec = [
    ('filename', nb.uint8[:]),
    ('rows', nb.int32),
    ('cls', nb.int32),
    ('L', nb.int32),
    ('H', nb.int32),
    ('checking', nb.int32[:]),
    ('enum_file', nb.int32[:]),
    ('step', nb.int32),
    ('slices', nb.int32),
]

@nb.jitclass(spec)
class A:
    def __init__(self, fname):
        self.filename = fname

and then:

a = A(np.frombuffer(b'abcdef', dtype='uint8'))
print(a.filename.tostring())

You won't be able to use tostring() in a nopython jitted function, but if you're just using it outside of numba, it works.



来源:https://stackoverflow.com/questions/48987368/how-can-i-pass-string-type-in-class-in-numba-jitclass-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!