Python Client Query - Get result Data


#1

Hello! Loving grakn.ai, but I am having a little trouble(my lack of understanding) with the python-client.
OS: macOS High Sierra
Grakn version: 1.4.2
Python Client version: 1.3.2
Keyspace: grakn

Running this query : “match $x isa person; offset 0; limit 1; get;”

iterator = tx.query("match $x isa person; offset 0; limit 1; get;")
result = next(iterator)
print(result)

I wish to get access to all key/val pairs, something like:

{
    "Node":{"id":"V49152", "type":"person", "baseType":"Entity"},
    "Attributes": {"firstname":"Bryon", "gender":"male", "identifier":"Bryon Culbertson", "surname":"Culbertson"}
}

Most things I have tried I just get an object:
<grakn.service.Session.Concept.Concept.Entity object at 0x1167725f8>

Any code suggestions or help would be great! Thanks!


#2

Hi there and welcome :slight_smile:

The objects you’re receiving are all types of Concept. If you check out the client-python docs https://dev.grakn.ai/docs/language-drivers/client-python (or on github here), and scroll down to the API reference you’ll see how to get the values you want.

To get the ID, you can just do result.id. You can access base type directly as result.base_type but its not recommended as much as checking via result.is_attribute() or result.entity_type() etc.

If you check out the methods on Thing objects, you’ll see you can get an iterator of Attribute objects using result.attributes(). Each Attribute object in turn has attribute.value() to obtain the value (just noticed an error in the docs, you don’t necessarily get a String but back whichever the datatype the attribtue was declared to be in the schema!)

Hope that helps!


#3

@joshua Thank you very much for the quick reply! So I was able to get the values, but I also want the “keys”, i.e. “firstname”, “gender” etc. Here is my current code:

iterator = tx.query("match $x isa person; offset 0; limit 1; get;")
concept_map = next(iterator)
print("ConceptMap Obj: {}".format(concept_map))

# Get Entity Obj from ConceptMap
entity = concept_map.map()['x']
print("Entity Obj: {}".format(entity))

print("Entity Id: {}".format(entity.id))

# Get Entity Attribute Values
attrs = entity.attributes()
for each in attrs:
    print(each.value())

Which outputs:

ConceptMap Obj: <grakn.service.Session.util.ResponseReader.ConceptMap object at 0x112174a58>
Entity Obj: <grakn.service.Session.Concept.Concept.Entity object at 0x111a405f8>
Entity Id: V41144
Niesz
Benjamin Franklin Niesz
male
Benjamin
Franklin
1842-11-15 19:03:58 

So how do I get the “key” for each attribute? Thanks!


#4

The “key” is what we refer to the “Label” of the attribute. The only gotcha is that the label only exists on the type of the attribute, rather than the attribute itself, so you have to do:

attribute.type().label()

tip: When writing code here you can use markdown, wrap code in backticks (`) for inline code or triple backticks (```) for code blocks :slight_smile:


#5

@joshua Perfect thanks!

For future people:

iterator = tx.query("match $x isa person; offset 0; limit 1; get;")
concept_map = next(iterator)
print("ConceptMap Obj: {}".format(concept_map))

# Get Entity Obj from ConceptMap
entity = concept_map.map()['x']
print("Entity Obj: {}".format(entity))
print("Entity Id: {}".format(entity.id))
 
# Get Entity Attribute Label/Values
attrs = entity.attributes()
for each in attrs:
     print(each.type().label())
     print(each.value())

OUTPUT:

ConceptMap Obj: <grakn.service.Session.util.ResponseReader.ConceptMap object at 0x113c10b00>
Entity Obj: <grakn.service.Session.Concept.Concept.Entity object at 0x113c107f0>
Entity Id: V41144
surname
Niesz
identifier
Benjamin Franklin Niesz
gender
male
firstname
Benjamin
middlename
Franklin
birth-date
1842-11-15 19:03:58

This is great, but… 6 API calls to get the “Lable/Value” of an Entity… A future feature request, could you create a single API call to do this…? Thanks again @joshua!