Python Client Question


#1

Hi,
I have just started to use the python client which works already very well.
I have moved some applications from batch import to the use of the REST API and one result was that I did get warnings on the server side. It looks like the python client does try a GET for queries and only a POST if this fails.
Simply using the _post method of the Graph class instead of the execute method for queries which write to the graph solved this (and increased the performance quite a bit) but I would rather not use protected methods to stay compatible with future versions and I saw a comment in the source which seems to indicate a planned changed for the API endpoint – is this planned for the nearer future?
Kind Regards


#2

Hi,

You’re right that the client currently attempts all endpoints in succession, because only certain endpoints support certain types of queries (and the client cannot know which to use without parsing the query).

In the next release (within a month) we will have a general Graql query endpoint instead and will only use that.

If there’s a performance benefit then I would use the protected _post method for now, but it may be removed in the future.


#3

hi @fab,

Please can you share the documents if any or insights on how you used python client for the same?

thank you…


#4

Hi,

Using the python client is actually quite simple but I have wrapped it a bit for my use cases so it is not easy to extract demonstration code.

The general idea is to setup a “Graph” from the python package maybe with something like

def __init__(self, host: str, port: str, keyspace: str):
    self.graph = Graph(uri="http://{}:{}".format(host, port), keyspace=keyspace)

I have written a small wrapper which generates query strings from a model description and the main point of the question was if it makes sense to use the private _post method instead of execute from the Graph object.

self.graph._post(myquery) or self.graph.execute(myquery) in the context of the demo snippet.
I am currently only using execute method because I want to use the public methods only to avoid problems with future updates.

I hope this helps.


#5

Hi @fab,

Thank you for the reply…

Please Let me know if i figured it wrong and please do correct me…
The steps i would follow that i understood from your reply…

1. create a class of the graph
2. make an object of it.
3. call the _post method on the object with the query string
4. retrieve the data in json format

Did i got it correct?
Also are you using any web framework?


#6

Hi,

I would

  • Create an instance of the grakn.client.Graph class

  • call the execute method on the instance you create with your query (using _post only works for some queries and is a private method)

  • use the response which is a requests.Response if I remember correctly. For simple tests you could start with

    from grakn.client import Graph, GraknError
    host = "Myhost"
    port = 4567
    keyspace = "grakn"
    graph = Graph(uri=“http://{}:{}”.format(host, port), keyspace=keyspace)
    resp = graph.execute(“YOUR QUERY HERE”)
    if resp.ok:
    …result = response.json()[‘response’]

This is from memory so please excuse if this is not complete but I hope it gives you the general idea.


#7

Hi @fab ,

Thank you for the amazing explanation…

I would really like to learn more and contribute if there is any plans for python documentation of using grakn.ai

Thanks a lot…:slight_smile:

I have implemented a simple sample in the python…following is the code for python grakn new users…using grakn ai at localhost

pip install grakn

from grakn.client import Graph, GraknError
host = 'localhost’
port = 4567
keyspace = 'grakn’
graph = Graph(uri=“http://{}:{}”.format(host, port), keyspace=keyspace)
try:
resp = graph.execute(“match $x isa person;”)
except:
print(“error”)
print(resp)

resp will be a list … following is the response that i got…

[{‘x’: {‘isa’: ‘person’, ‘id’: ‘57344’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘57432’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘77912’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘81992’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘90352’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘98304’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘98392’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘102488’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘110608’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘110680’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘119024’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘122880’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘122936’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘123120’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘127200’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘135408’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘143584’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘147472’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘147528’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘151640’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘151704’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘159800’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘163928’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘172032’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘176144’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘180280’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘180416’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘188568’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘188640’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘192664’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘192704’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘192752’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘209120’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘209136’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘213080’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘217144’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘221256’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘225352’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘229376’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘229448’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘233664’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘237640’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘241736’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘245984’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘254104’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘258240’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘262200’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘266432’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘266480’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘270424’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘278544’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘295064’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘315408’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘319712’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘385248’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘389344’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘434400’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘442592’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘458976’}}, {‘x’: {‘isa’: ‘person’, ‘id’: ‘479456’}}]

A special thanks @fab …thank you… :slight_smile:

*this was done in python REPL, and worked fine…but while doing in as a filenam.py way headers exception is fired…
_HEADERS: Dict[str, str] = {‘Accept’: ‘application/graql+json’}

@fab
please can you guide me for this exception?
any suggestions for this?