Owlready2 dynamic class generation

人走茶凉 提交于 2020-01-05 07:10:30

问题


I am trying to dynamically create a class for an owlready2 ontology. The documentation suggests the following line of code:

NewClass = types.new_class("NewClassName", (SuperClass,), kwds = { "namespace" : my_ontology })

In my case this equals

types.new_class("NewClassName", (onto["ParentClass"],), kwds={'namespace' : onto})

However, when I run the above code, I get the following exception:

 Traceback (most recent call last):
    (onto[object.get('owl_dataProperty_parent')],), kwds={'namespace' : onto})
    File "/usr/lib/python3.6/types.py", line 62, in new_class
    return meta(name, bases, ns, **kwds)
 TypeError: __new__() got an unexpected keyword argument 'namespace

I have no idea what went wrong there and after hours of debugging I am still clueless. I am using Python 3.6.6 and version 0.11 of owlready2


回答1:


I figured out that the "namespace" attribute is not needed at all. So the following works just fine:

types.new_class("NewClassName", (onto["ParentClass"],))

Even though this does not solve the overall issue, it answered my question.

Additionally, it seams like this is the way to do it in general, cf.owlready forum. So maybe it is time to update the documentation.



来源:https://stackoverflow.com/questions/53397615/owlready2-dynamic-class-generation

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