Gremlin Python 3.5.0 features and updates

237 views
Skip to first unread message

Kelvin Lawrence

unread,
May 20, 2021, 12:34:44 PM5/20/21
to Gremlin-users
 Lately I find myself doing a lot of Gremlin query writing in Python using the gremlin-python client. In the TinkerPop 3.5.0 release there are some nice updates for Python users.

Tornado replaced 

Since release, the Python client has used Tornado for its websocket and other transport needs. While this generally worked well, in some cases, such as when working within Jupyter notebooks, the Gremlin client encountered conflicts when trying to create its event loop. Starting with the 3.5.0 release, the Python client no longer uses Tornado. Instead AIO HTTP is used. For most users this will have zero impact to their code. However any code using features provided by Tornado may have to be slightly revised to use AIO HTTP instead. The move to AIO HTTP removes the event loop issues experienced by users of the prior clients and opens the door for additional future improvements.

max_content_length 

The Gremlin Java clients provide a setting that can be used to increase, or decrease, the maximum size of the result that can be returned to a client. The Tornado default is 10,240 bytes ( 10 * 10 * 1024) but the prior Python clients did not expose a way to set this value. Starting with the 3.5.0 Python client, transport specific arguments can now be provided when a remote connection is created. These are passed as kwargs to allow for easy expansion in the future without having to add additional arguments to the API each time a new setting is needed. If a value is not provided for max_content_length the same default 10,0240 byte value is used. The code below shows an example of how this works.

server = 'localhost'
port = 8182
endpoint = 'ws://' + server + ':' + str(port) + '/gremlin'
transport_args = {'max_content_length': 5000}
connection = DriverRemoteConnection(endpoint, 'g', **transport_args)
g = traversal().withRemote(connection)
results = g.V('1','2','3').valueMap().toList()
print(results)

New HashableDict class 

Prior to the 3.5.0 client, a query such as the one shown below will fail as it returns a dict (dictionary/map) result type where the keys are themselves dicts. Python does not allow that and will throw an exception if you try it. This is because in Python, keys for dicts must be immutable so that they can be hashed. Behind the scenes, the 3.5.0 Python client introduces a HashableDict class that handles these cases. So queries like this one will now work.

result = g.V(*'1'*,*'2'*).group().by(__.project(*'a'*).by(__.constant(*'x'*))).next()
print(result)

{{'a': 'x'}: [v[1], v[2]]}

Translator 

The 3.5.0 client adds a Translator class that can turn traversals back into Groovy style Gremlin plain text strings. This is often useful for cases such as logging, debugging and working with Gremlin sessions where queries have to be sent as text strings.

from gremlin_python.process.translator import *
...

t = g.V().hasLabel('airport').out().count()
tlr = Translator().of('g')
print(tlr.translate(t.bytecode))


g.V().hasLabel('airport').out().count()


Python 2 support discontinued 

The 3.5.0 client no longer supports Python 2. Most users by now are likely already using Python 3.x and dropping support for Python 2 will make it easier to add features to the Gremlin Python client in the future.

Further reading 

All of the changes in this release, as always, can be found in the change log at https://github.com/apache/tinkerpop/blob/master/CHANGELOG.asciidoc
Reply all
Reply to author
Forward
0 new messages