Discussion Forum

Getting results back from Python client

Getting results back from Python client

from grakn.client import GraknClient

with GraknClient(uri="localhost:48555") as client:
    with client.session(keyspace="test") as session:
        ## Or query and consume the iterator immediately collecting all the results
        with session.transaction().read() as read_transaction:
            answer_iterator = read_transaction.query("match $x isa timeframe, has Fiscal_Week_Aging > 233; get $x;").get()
            tf = [ans.get() for ans in answer_iterator]
            for person in tf:
                print(person.Fiscal_Week_Aging)

person is of type grakn.service.Session.util.ResponseReader.ConceptMap
But i am not able to see actual values:
person.Fiscal_Week_Aging or person.get(“Fiscal_Week_Aging”) all are throwing error

Hi @dipranjan!

It’s important to know that Grakn attributes are nodes of their own - they don’t exist on entities like you might imagine. Therefore, attributes are not retrieved unless you specifically ask for them. There are two ways to retrieve them.

First is to change your query to explicitly ask for an attribute as a variable. The variables that you state are what you will get back in your answer, so let’s give Fiscal_Week_Aging a variable $fwa:

answer_iterator = read_transaction.query("match $x isa timeframe, has Fiscal_Week_Aging $fwa; $fwa > 233; get $x, $fwa;"
for answer in answer_iterator:
  print(answer.get().get("fwa").value())

Alternatively, you can use ConceptAPI to get the attributes of a concept. Here we’ll get an attribute of the timeframe concept (people aren’t present in your query which may need fixing…):

answer_iterator = tx.query("match $x isa timeframe, has Fiscal_Week_Aging > 233; get $x, $fwa;"
fwa_type = tx.get_schema_concept("Fiscal_Week_Aging")
for answer in answer_iterator:
  fwa_iterator = answer.get().get("x").asRemote(tx).attributes(fwa_type)
  for fwa in fwa_iterator:
    print(fwa.value())

This code is untested!

Hope this helps

@james Thanks for the reply. This helped, was able to get the required data. I am just getting started with Grakn so still stuggling with the features and functionalities.