How to create and assign instance properties values from a list

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

How to create and assign instance properties values from a list

TamiM
Hello Jiba,
Thank you so much for 'Owlready2'.  I have problems while trying to assign dynamically instance data property values from lists. I tried to define/call each data property implicitly as 'onto_data_property', but I get the error:

"AttributeError: 'onto_data_property' property is not defined."

How can I assign these instance DataProperties dynamically from a list of values?
Very much obliged for your help.

Please find below an example:
----------------------------------------------
people = ["Tom", "Dan","Nick"]
s = ["has_A", "has_B", "has_C"]
word=["street", "20", "email"]
...
    # Have added instances to Class "student" from the list "people".
...
    # Created Data Properties associated to class "student" from the list "s":
...

    ind = onto.student(people[2])                              ## instance in class "student"
    list_prop = [ind.has_A, ind.has_B, ind.has_C]          ## 1) explicitly defining data properties
   # list_prop = ind.onto_data_property                     ## 2) attempt to implicit call

    if(word[0] not in list_prop[0]):list_prop[0].append(word[0])

----------------------------------------------
Result 1: explicitly defining data properties - gives the expected result:

<student rdf:about="#Nick">
  <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
  <has_A rdf:datatype="http://www.w3.org/2001/XMLSchema#string">street</has_A>
</student>

-----------------------------------------------
Result 2:  ## attempt to implicit call
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-bb711b2c3564> in <module>
 
----> 3 list_prop = ind.onto_data_property
      4 if(word[0] not in list_prop[0]):list_prop[0].append(word[0])
      5

~\AppData\Local\Continuum\Anaconda3\lib\site-packages\owlready2\individual.py in __getattr__(self, attr)
    243       if not Prop:
    244         if attr == "equivalent_to": return self.get_equivalent_to() # Needed
--> 245         raise AttributeError("'%s' property is not defined." % attr)
    246       if Prop.is_functional_for(self.__class__): self.__dict__[attr] = r = Prop._get_value_for_individual (self)
    247       else:                                      self.__dict__[attr] = r = Prop._get_values_for_individual(self)

AttributeError: 'onto_data_property' property is not defined.
-------------------------------------------------------------------------


Reply | Threaded
Open this post in threaded view
|

Re: How to create and assign instance properties values from a list

Jiba
Administrator
Hello,

I think what you need is the getattr() Python method (which works with any Python object including those from Owlready).

For example:

people_name = "jiba"
prop_name = "has_A"
prop_value = "street"

ind = onto.student(people_name)
getattr(ind, prop_name).append(prop_value)

Notice that for functional properties, you need setattr():

setattr(ind, prop_name, prop_value)


Alternatively, you can also use this syntax which works for both functional and non functional properties :

prop = onto.has_A # or getattr(onto, prop_name)
prop[ind].append(prop_value)


Jiba
Reply | Threaded
Open this post in threaded view
|

Re: How to create and assign instance properties values from a list

TamiM
Hello Jiba,
Your solution works perfectly! The key is in using:

 

getattr(ind, prop_name).append(prop_value)


This is Great! I've tried for days various alternatives, but could not think of this.
Very many thanks,
Tami