How to be Pythonic? Design a Query Language in Python Cheuk Ting - - PowerPoint PPT Presentation

how to be pythonic design a query language in python
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

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

slide-2
SLIDE 2
slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5

https://www.twitch.tv/cheukting_ho

slide-6
SLIDE 6

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

slide-7
SLIDE 7

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

slide-8
SLIDE 8

“ 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.

slide-9
SLIDE 9

“ 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
slide-10
SLIDE 10

“ 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
slide-11
SLIDE 11

“ 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?

slide-12
SLIDE 12

for i in (i; i < items.length ; i++) { n = items[i]; ... now do something } 1 2 3 4 5

slide-13
SLIDE 13

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

slide-14
SLIDE 14

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

slide-15
SLIDE 15

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

🐎

Pythonic!

slide-16
SLIDE 16

Design a Query Language in Python

slide-17
SLIDE 17

Design a Query Language in Python

slide-18
SLIDE 18

It all stated...

Co-organizer of Open Source contribution Creator of Volenteer of Developer Advocate of

slide-19
SLIDE 19
slide-20
SLIDE 20
slide-21
SLIDE 21

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

slide-22
SLIDE 22

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

slide-23
SLIDE 23
slide-24
SLIDE 24

🤕

slide-25
SLIDE 25

🤕

💢

slide-26
SLIDE 26

a Query Language Client for Pythonistas and Data Scientists

WOQLpy

🤕

💢

slide-27
SLIDE 27

a Query Language Client for Pythonistas and Data Scientists

WOQLpy

🤕

💢

👐&&&& &&&

slide-28
SLIDE 28

What is WOQLpy?

slide-29
SLIDE 29

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

pip install terminusdb-client 1

slide-30
SLIDE 30

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

🐽

slide-31
SLIDE 31

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

slide-32
SLIDE 32

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

slide-33
SLIDE 33

You can do both

(WOQLQuery().doctype("Station") .label=("Bike Station") .description("A station where bikes are deposited") ) 1 2 3 4

  • r

WOQLQuery().doctype("Station", label="Bike Station", escription="A station where bikes are deposited") 1 2 3

slide-34
SLIDE 34

Which one do you prefer?

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

  • r

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

slide-35
SLIDE 35

Which one do you prefer?

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

  • r

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

slide-36
SLIDE 36

Design challanges

JavaScript: WOQL.and() Python: WOQLQuery().and() ?

slide-37
SLIDE 37

Design challanges

JavaScript: WOQL.and() Python: WOQLQuery().and() ? "and" is a key word, you dummy!

slide-38
SLIDE 38

Design challanges

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...

slide-39
SLIDE 39

Look into the future

slide-40
SLIDE 40
slide-41
SLIDE 41

Can we have a nice graph visulization?

slide-42
SLIDE 42

Load data from DataFrame🐽

slide-43
SLIDE 43

Load data from DataFrame🐽 CLI client (with click?)

slide-44
SLIDE 44

Load data from DataFrame🐽 Many more fail-proof checks e.g. check user inputs, check database version etc... CLI client (with click?)

slide-45
SLIDE 45

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