Dynamic Class Creation - got an unexpected keyword argument 'namespace'

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Dynamic Class Creation - got an unexpected keyword argument 'namespace'

Peter
Dear List
The code below returns the following error:

return meta(name, resolved_bases, ns, **kwds)
TypeError: __new__() got an unexpected keyword argument 'namespace'

https://stackoverflow.com/questions/53397615/owlready2-dynamic-class-generation refers to the same problem and provides a solution

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

What are all the optional parameters for this method of creating new classes dynamically please?

TIA

========================
from owlready2 import *
import types

ont = get_ontology("http://base.org/iri")
onto = get_ontology("http://base.org/iri2")

with ont:
    class Sup(Thing):
        pass

NewClass = types.new_class("NewClassName", (Sup,), kwds = {"namespace" : onto})




Reply | Threaded
Open this post in threaded view
|

Re: Dynamic Class Creation - got an unexpected keyword argument 'namespace'

Jiba
Administrator
Hi,

The full syntax is:

NewClass = types.new_class("NewClassName", (superclass1, superclass2), kwds = {"namespace" : onto})

or (probably preferable):

with onto:
        NewClass = types.new_class("NewClassName", (superclass1, superclass2))


For the superclass, you MUST pass one superclass that is a descendant of Thing, or pass Thing otherwise (if you don't, you will create a normal Python class and not an Owlready OWL class). If there is a single superclass, you must pass it as a tuple: (superclass,).

Jiba

Reply | Threaded
Open this post in threaded view
|

Re: Dynamic Class Creation - got an unexpected keyword argument 'namespace'

Peter
Thanks for the code Jiba
I have an issue:

Running the following gives me an error

#---------------code-----------
from owlready2 import *

onto = get_ontology("http://test.org/onto.owl")
with onto:
    class superclass1(Thing):
        pass
    class superclass2(Thing):
        pass

NewClass = types.new_class("NewClassName", (superclass1, superclass2), kwds = {"namespace" : onto})

default_world.save()

onto.save(file=r".\test1.rdf", format="rdfxml")


========ERROR RETURNED===========

C:\XXXXXXXXX\venv\Scripts\python.exe C:/Users/Pedro/PycharmProjects/cpov-ap-scot/test1.py
Traceback (most recent call last):
  File "C:/XXXXXXX/test1.py", line 10, in <module>
    NewClass = types.new_class("NewClassName", (superclass1, superclass2), kwds = {"namespace" : onto})
  File "C:\Python\Python37-32\lib\types.py", line 70, in new_class
    return meta(name, resolved_bases, ns, **kwds)
TypeError: __new__() got an unexpected keyword argument 'namespace'

Process finished with exit code 1
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic Class Creation - got an unexpected keyword argument 'namespace'

Jiba
Administrator
It seems that I was wrong: only the second syntax is valid (using with).

So you can do:

from owlready2 import *

onto = get_ontology("http://test.org/onto.owl")
with onto:
    class superclass1(Thing):
        pass
    class superclass2(Thing):
        pass

with onto:
    NewClass = types.new_class("NewClassName", (superclass1, superclass2))

onto.save(file=r"/tmp/test1.owl", format="rdfxml")



(NB you don't need default_world.save(), unless you are storing the quadstore in a file).

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic Class Creation - got an unexpected keyword argument 'namespace'

Peter
That's really helpful!  Many thanks
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic Class Creation - got an unexpected keyword argument 'namespace'

Matthias Samwald
In reply to this post by Jiba
This should be updated in the documentation, which only demonstrates the first (unfortunately not working) variant.

Also it would be great if e.g. class labels could also be added right during dynamic class creation! :)
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic Class Creation - got an unexpected keyword argument 'namespace'

cdemir
+1