Код: Выделить всё
attributs_data = data.get("attributs", {}) # here i recover JSON data
if isinstance(attributs_data, dict):
attributs = Attributs()
document.has_attributs = attributs
for key, value in attributs_data.items():
prop_name = f"has_{key}"
# Define the property dynamically within the ontology context
with onto:
# here i try every type of data to create de class
if isinstance(value, str):
prop_cls = types.new_class(prop_name, (FunctionalProperty,),
kwds={"domain": [Attributs], "range": [str]})
elif isinstance(value, dict):
nested_cls = types.new_class(key.replace(" ", "_").capitalize(), (Thing,))
nested_instance = nested_cls()
setattr(attributs, prop_name, nested_instance)
for subkey, subvalue in value.items():
nested_prop_name = f"has_{subkey}"
with onto:
if isinstance(subvalue, str):
nested_prop_cls = types.new_class(nested_prop_name, (FunctionalProperty,),
kwds={"domain": [nested_cls], "range": [str]})
elif isinstance(subvalue, int):
nested_prop_cls = types.new_class(nested_prop_name, (FunctionalProperty,),
kwds={"domain": [nested_cls], "range": [int]})
elif isinstance(subvalue, float):
nested_prop_cls = types.new_class(nested_prop_name, (FunctionalProperty,),
kwds={"domain": [nested_cls], "range": [float]})
elif isinstance(subvalue, bool):
nested_prop_cls = types.new_class(nested_prop_name, (FunctionalProperty,),
kwds={"domain": [nested_cls], "range": [bool]})
setattr(nested_instance, nested_prop_name, subvalue)
Код: Выделить всё
File "jsonToOwl.py", line 115, in
prop_cls = types.new_class(prop_name, (FunctionalProperty,),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/types.py", line 74, in new_class
return meta(name, resolved_bases, ns, **kwds)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: EntityClass.__new__() got an unexpected keyword argument 'domain'
Если бы у кого-то возникла идея, я бы вышла за него замуж< /п>
Подробнее здесь: https://stackoverflow.com/questions/785 ... -in-python