How to be Pythonic? Design a Query Language in Python

Β 

What does Pythonic means? (Is it a thing?)

Β 

http://etc.ch/6tnU

Pythonic means code that doesn't just get the syntax right but that follows the conventions of the Python community and uses the language in the way it is intended to be used.

- Stackoverflow

🐼

Why can't I just do it in a for-loop?

for i in (i; i < items.length ; i++)
 {
  n = items[i];
 ... now do something
 }
for i in items:
  i.perform_action()
(i.some_attribute for i in items)

🐍

Pythonic!

Design a Query Language in Python

Β 

http://etc.ch/6tnU

It all stated...

Co-organizer ofΒ 

Open Source contribution

Creator of

Volenteer of

Developer Advocate of

SELECT Name from TABLE where Person_ID = (SELECT mother from TABLE where Name="John")
SELECT Name from TABLE where Person_ID = (SELECT mother from TABLE WHERE Person_ID = (SELECT mother from TABLE where Name="John"))
WOQL.and(
   WOQL.triple("v:Person", "mother", "v:MotherID"),
   WOQL.triple("v:MotherID", "name", "v:MotherName"),
   WOQL.triple("v:MotherID", "mother", "v:GrandmotherID"),
   WOQL.triple("v:GrandmotherID", "name", "v:GrandmotherName"),
)

a Query Language Client for Pythonistas and Data Scientists

WOQLpy

πŸ€”

πŸ’‘

πŸ‘πŸ‘πŸ‘πŸ‘πŸ‘πŸ‘πŸ‘πŸ‘

What is WOQLpy?

It let's you to "talk" to TerminusDB like this:

import woqlclient.woqlClient as woql
from woqlclient import WOQLQuery

server_url = "http://localhost:6363"
key = "root"
dbId = "pybike"
client = woql.WOQLClient()
client.connect(server_url, key)
client.createDatabase(dbId, "Bicycle Graph")
station = WOQLQuery().doctype("Station").label("Bike Station")
journey = WOQLQuery().doctype("Journey")
journey = journey.label("Journey")
journey = journey.property("start_station", "Station").label("Start Station")
journey = journey.property("end_station", "Station").label("End Station")
schema = WOQLQuery().when(True).woql_and(station, journey)
schema.execute(client)

Instead of this:

{
  "when": [
    {
      "true": []
    },
    {
      "and": [
        {
          "add_quad": [
            "scm:Station",
            "rdf:type",
            "owl:Class",
            "db:schema"
          ]
        },
        {
          "add_quad": [
            "scm:Station",
            "rdfs:subClassOf",
            "tcs:Document",
            "db:schema"
          ]
        },
        {
          "add_quad": [
            "scm:Station",
            "rdfs:label",
            {
              "@value": "Bike Station",
              "@language": "en"
            },
            "db:schema"
          ]
        },
        {
          "add_quad": [
            "scm:end_station",
            "rdf:type",
            "owl:ObjectProperty",
            "db:schema"
          ]
        },
        {
          "add_quad": [
            "scm:end_station",
            "rdfs:range",
            "scm:Station",
            "db:schema"
          ]
        },
        {
          "add_quad": [
            "scm:end_station",
            "rdfs:domain",
            "scm:Journey",
            "db:schema"
          ]
        },
        {
          "and": [
            {
              "add_quad": [
                "scm:start_station",
                "rdf:type",
                "owl:ObjectProperty",
                "db:schema"
              ]
            },
            {
              "add_quad": [
                "scm:start_station",
                "rdfs:range",
                "scm:Station",
                "db:schema"
              ]
            },
            {
              "add_quad": [
                "scm:start_station",
                "rdfs:domain",
                "scm:Journey",
                "db:schema"
              ]
            },
            {
              "and": [
                {
                  "add_quad": [
                    "scm:Journey",
                    "rdf:type",
                    "owl:Class",
                    "db:schema"
                  ]
                },
                {
                  "add_quad": [
                    "scm:Journey",
                    "rdfs:subClassOf",
                    "tcs:Document",
                    "db:schema"
                  ]
                },
                {
                  "add_quad": [
                    "scm:Journey",
                    "rdfs:label",
                    {
                      "@value": "Journey",
                      "@language": "en"
                    },
                    "db:schema"
                  ]
                }
              ]
            },
            {
              "add_quad": [
                "scm:start_station",
                "rdfs:label",
                {
                  "@value": "Start Station",
                  "@language": "en"
                },
                "db:schema"
              ]
            }
          ]
        },
        {
          "add_quad": [
            "scm:end_station",
            "rdfs:label",
            {
              "@value": "End Station",
              "@language": "en"
            },
            "db:schema"
          ]
        }
      ]
    }
  ]
}

Which one do you prefer?

Chaining:
WOQLQuery().doctype("journey").label("Journey)

or

Multi-parameters:
WOQLQuery().doctype(id="journey, label="Journey")

http://etc.ch/6tnU

It comes with the Python Client, which you can pip install:

pip install terminus-client-python

**Newly added** Output to DataFrames

pip install terminus-client-python[dataframe]
woql.query_to_df(result)

Change the result returned form your query into pandas DataFrame

🐼

Design challanges

JavaScript: WOQL.and()

Python: WOQLQuery().and() ?

"and" is a key word, you dummy!

OK, woql_and then....πŸ˜“

WOQLQuery().woql_and()

also happened to: or, not, as, from...

Look into the future

Can we have this is Jupyter Notebook?Β 

Load data from DataFrame

🐼

Many more fail-proof checks
e.g. check user inputs,
check database version etc...

Webinar:
Dataops 101 (https://events.terminusdb.com/)
31th March - 12noon CET
(11am BST and IST/ 10amUTC, I did check, ok?)

Β 

To get the newest updateπŸ‘:

Follow us on Twitter: @TerminusDB

Website: https://terminusdb.com/

Join the community:

https://community.terminusdb.com/

We want to hear from you 😊

How to be Pythonic? Design a Query Language in Python

By Cheuk Ting Ho

How to be Pythonic? Design a Query Language in Python

  • 1,018