How to be Pythonic? Design a Query Language in Python
Cheuk Ting Ho
@cheukting_ho https://cheuk.dev Cheukting
Grab the slides: bit.ly/be-pythonic
How to be Pythonic? Design a Query Language in Python Cheuk Ting - - PowerPoint PPT Presentation
Grab the slides: bit.ly/be-pythonic How to be Pythonic? Design a Query Language in Python Cheuk Ting Ho https://cheuk.dev Cheukting @cheukting_ho https://www.twitch.tv/cheukting_ho What does Pythonic mean? (Is it a thing?) What does
Cheuk Ting Ho
@cheukting_ho https://cheuk.dev Cheukting
Grab the slides: bit.ly/be-pythonic
https://www.twitch.tv/cheukting_ho
“ 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.
“ 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.
“ 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.
“ 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.
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 } 1 2 3 4 5
for i in (i; i < items.length ; i++) { n = items[i]; ... now do something } 1 2 3 4 5 for i in items: i.perform_action() 1 2
for i in (i; i < items.length ; i++) { n = items[i]; ... now do something } 1 2 3 4 5 for i in items: i.perform_action() 1 2 (i.some_attribute for i in items) 1
for i in (i; i < items.length ; i++) { n = items[i]; ... now do something } 1 2 3 4 5 for i in items: i.perform_action() 1 2 (i.some_attribute for i in items) 1
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")) 1 2
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"), ) 1 2 3 4 5 6
a Query Language Client for Pythonistas and Data Scientists
WOQLpy
a Query Language Client for Pythonistas and Data Scientists
WOQLpy
👐&&&& &&&
It comes with the Python Client, which you can pip install:
pip install terminusdb-client 1
It comes with the Python Client, which you can pip install:
pip install terminusdb-client 1
**Newly added** Output to DataFrames
pip install terminusdb-client[dataframe] 1 woql.query_to_df(result) 1
Change the result returned form your query into pandas DataFrame
It let's you to "talk" to TerminusDB like this:
import terminusdb_client as woql from terminusdb_client import WOQLQuery db_id = "pybike" client = woql.WOQLClient(server_url = "http://localhost:6363") client.connect(key="root", account="admin", user="admin") client.create_database(db_id, accountid="admin", label = "Bike Graph", description = "Create a graph with bike data") station_dt = WOQLQuery().doctype("Station", label="Bike Station", description="A station where bikes are deposited") bicycle_dt = WOQLQuery().doctype("Bicycle", label="Bicycle") journey_dt = ( WOQLQuery().doctype("Journey", label="Journey"). property("start_station", "Station", label="Start Station"). property("end_station", "Station", label="End Station"). property("duration", "integer", label="Journey Duration"). property("start_time", "dateTime", label="Time Started"). property("end_time", "dateTime", label="Time Ended"). property("journey_bicycle", "Bicycle", label="Bicycle Used") ) schema = station_dt + bicycle_dt + journey_dt schema.execute(client) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
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" ] }, { 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
You can do both
(WOQLQuery().doctype("Station") .label=("Bike Station") .description("A station where bikes are deposited") ) 1 2 3 4
WOQLQuery().doctype("Station", label="Bike Station", escription="A station where bikes are deposited") 1 2 3
Which one do you prefer?
Chaining: WOQLQuery().doctype("journey").label("Journey)
Multi-parameters: WOQLQuery().doctype("journey, label="Journey")
Which one do you prefer?
Chaining: WOQLQuery().doctype("journey").label("Journey)
Multi-parameters: WOQLQuery().doctype("journey, label="Journey")
JavaScript: WOQL.and() Python: WOQLQuery().and() ?
JavaScript: WOQL.and() Python: WOQLQuery().and() ? "and" is a key word, you dummy!
JavaScript: WOQL.and() Python: WOQLQuery().and() ? "and" is a key word, you dummy! OK, woql_and then....😔 WOQLQuery().woql_and() * actually you can use the + operator thanks for the overload ability in Python also happened to: or, not, as, from...
Can we have a nice graph visulization?
Load data from DataFrame🐽
Load data from DataFrame🐽 CLI client (with click?)
Load data from DataFrame🐽 Many more fail-proof checks e.g. check user inputs, check database version etc... CLI client (with click?)
World of WoqlCraft: Every Friday 5pm UK time / 6pm CET To get the newest update👎: Follow us on Twitter: Website: Join the community at Discord: We want to hear from you 😋 @TerminusDB https://terminusdb.com/ https://discord.gg/Gvdqw97