Owlready2 dynamic class generation

2019-08-18 03:19发布

问题:

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.