Skip to content

Developing with Python

In the aerospike-graph directory, the python_example.py app demonstrates useful development patterns for connecting to an AGS deployment and adding vertices when building a proof of concept application.

Explore the Python example

With AGS running, let’s test things out using the Python example app.

  1. Set up a Python virtual environment:

    Terminal window
    python3 -m venv .venv
    source .venv/bin/activate
  2. Open the Python example app:

    Terminal window
    open ./python_example/python_example.py

    Let’s pause for a moment and understand Gremlin syntax. A typical Gremlin command is a chain of traversal steps separated by dots, read from left to right.

    It starts with a graph traversal source declaration like:

    Terminal window
    GraphTraversalSource g = traversal().withRemote(conn);
    g.V().has('property', 'value').out('edgeLabel').values('anotherProperty')

    On the second line, you can see our Gremlin command. Let’s break down each step:

    g.V() uses the traversal source declared previously as g and then uses V() to start with all vertices.

    .has('property', 'value') filters by a property.

    .out('edgeLabel') traverses outgoing edges.

    .values('anotherProperty') gets property values.

    To reiterate, each step processes or filters the graph elements from the previous step.


    This will make more sense when looking at a real example. In the example app, lines 15-17 show the following:

    python_example.py
    g.add_v('foo').\
    property('company', 'aerospike').\
    property('scale', 'unlimited').iterate()

    g.add_v('foo') adds a vertex labeled "foo" and .property() is used twice to append two properties for this vertex.

    The python_example.py begins by importing DriverRemoteConnection and traversal creating a connection with the AGS server. Read through the code comments to understand how Gremlin commands connect to the server, add a vertex, update a property, and delete a vertex.

  3. Install the required packages and run the example.

    Terminal window
    python3 -m pip install gremlinpython async_timeout
    python3 ./python_example/python_example.py
    Example response
    Terminal window
    Values:
    ['aerospike', 'unlimited']
    Updated:
    ['aerospike', 'infinite']
Feedback

Was this page helpful?

What type of feedback are you giving?

What would you like us to know?

+Capture screenshot

Can we reach out to you?