From f5b94931274107ae70587a4c1ac0c3756b7f16ab Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Tue, 16 Mar 2021 00:32:48 +0100 Subject: Move reveal.js/sqlalchemy.html to reveal.js/dataaccess.html and make a new reveal.js/sqlalchemy.html --- reveal.js/sqlalchemy.html | 115 ++++++++++++++++++++++++++-------------------- 1 file changed, 66 insertions(+), 49 deletions(-) (limited to 'reveal.js/sqlalchemy.html') diff --git a/reveal.js/sqlalchemy.html b/reveal.js/sqlalchemy.html index 4bafccd..578fc13 100755 --- a/reveal.js/sqlalchemy.html +++ b/reveal.js/sqlalchemy.html @@ -2,7 +2,7 @@ - SQLAlchemy & Odin Data Access + SQLAlchemy @@ -11,7 +11,7 @@ - + @@ -24,8 +24,8 @@
-

SQLAlchemy & Odin Data Access

-

Fifty Data Science

+

SQLAlchemy

+

Data Science @ Statnett


Simeon Simeonov

@@ -38,7 +38,6 @@ @@ -56,18 +55,20 @@

Why use SQLAlchemy?


Basic architecture

-

SQLAlchemy consists of several components, including the the ORM.

+

SQLAlchemy consists of several components, including the ORM.

@@ -251,7 +251,8 @@ session.add(author_2) session.add(book_1) session.add(book_2) - session.add(book_3) # or simply session.add_all([author_1, author_2, book_1, book_2, book_3]) + session.add(book_3) + # or simply session.add_all([author_1, author_2, book_1, book_2, book_3]) # session.flush() session.commit() # flushes (issues the statements and sends them to the RDBMS) and commits @@ -270,61 +271,77 @@

Queries

             
-              session.query(Book).order_by(Book.id)  # returns a Query instance with a .statement attribute
-              session.query(Book).order_by(Book.id).all()  # returns an object-list
+              session.query(Book).order_by(Book.book_id)  # returns a Query instance with a .statement attribute
+              session.query(Book).order_by(Book.book_id).all()  # returns an object-list
 
               # return all book objects where title == "The Selfish Gene"
-              session.query(Book).filter(Book.title == "The Selfish Gene").order_by(Book.id).all()
+              session.query(Book).filter(Book.title == "The Selfish Gene").order_by(Book.book_id).all()
 
               # using LIKE
-              session.query(Book).filter(Book.title.like("The%")).order_by(Book.id).all()
+              session.query(Book).filter(Book.title.like("The%")).order_by(Book.book_id).all()
 
-              query = session.query(Book).filter(Book.id == 9).order_by(Book.id)
-              query.count()  # returns 0L
+              query = session.query(Book).filter(Book.book_id == 9).order_by(Book.book_id)
+              query.count()  # returns 0
               query.all()  # returns an empty list
               query.first()  # returns None
               query.one()  # raises NoResultFound exception
 
-              query = session.query(Book).filter(Book.id == 1).order_by(Book.id)
+              query = session.query(Book).filter(Book.book_id == 1).order_by(Book.book_id)
               book_1 = query.one()
               book_1.description  # returns "A popular science book"
               book_1.author.books  # returns a list of Book-objects representing all the books from the same author.
 
               # get a list of all Book-instances where the author"s name is "Richard Dawkins"
-              session.query(Book).filter(Book.author_id == Author.id).filter(Author.name == "Richard Dawkins").all()
+              session.query(Book).filter(Book.author_id == Author.author_id).filter(Author.name == "Richard Dawkins").all()
               session.query(Book).join(Author).filter(Author.name == "Richard Dawkins").all()
               session.query(Book).\
-                  from_statement("SELECT b.* FROM books b, authors a WHERE b.author_id = a.id AND a.name=:name").\
+                  from_statement("SELECT b.* FROM books b, authors a WHERE b.author_id = a.author_id AND a.name=:name").\
                   params(name="Richard Dawkins").all()
             
           
-

... simply amazing!

+

... there is more???

-

Odin Data Access

-

https://gitlab.fifty.eu/odin/data-science/odin-data-access

-

Provides the odin_data_access module with some of the following entities:

- -

The DBTableAccessor:

- -
+

Some nice features

+
+            
+              import pandas as pd
 
-        
-

Demo

-

Now watch this well-rehearsed demo...

+ from sqlalchemy import func + + class Book(Base): + # ... + author = relationship( + Author, backref=backref("books", lazy="dynamic", order_by=title) + ) + # ... + + @hybrid_property + def newly_arrived(self): + return self.book_id > 2 + + @newly_arrived.expression + def newly_arrived(cls): + return cls.book_id > 2 + # return func.abs(cls.book_id) > 2 + + # .books is now a Query object + query = author_obj.books.filter(Book.title.ilike("%red%")) + + session.query(Book).filter(Book.newly_arrived.is_(True)).all() + # Out: [<__main__.Book at 0x7f132bdf0130>] + # WHERE (abs(books.book_id) > ?) IS 1 ... in the case of func.abs + + df = pd.read_sql_table("my_table", con=session.get_bind()) # or con=engine + + df = pd.read_sql_query(query.statement, engine) + +
+
-- cgit v1.3